MIXAL
All Classes Files Functions Variables Typedefs Enumerations Friends Pages
io.cpp
1 #include <iostream>
2 #include <cstdlib>
3 #include <cmath>
4 #include <algorithm>
5 #include <sstream>
6 #include "io.h"
7 
8 namespace mixal {
9 
10 IODevice::IODevice(int32_t blockSize, bool allowRead, bool allowWrite) : _type(IODeviceType::TAPE),
11  _blockSize(blockSize), _allowRead(allowRead), _allowWrite(allowWrite),
12  _timestamp(), _readyRate(1.0) {}
13 
14 bool IODevice::ready(int32_t timestamp) {
15  int32_t elapsed = std::max(0, timestamp - _timestamp);
16  _timestamp = timestamp;
17  double r = static_cast<double>(rand()) / RAND_MAX;
18  double successRate = 1.0 - pow(1.0 - _readyRate, elapsed);
19  return r <= successRate;
20 }
21 
22 IODeviceStorage::IODeviceStorage(int32_t storageSize) : IODevice(100, true, true),
23  _status(IODeviceStatus::READY), _address(0), _locator(0), _memory(nullptr),
24  _buffer(100), _storage(storageSize) {}
25 
26 bool IODeviceStorage::ready(int32_t elapsed) {
27  bool state = IODevice::ready(elapsed);
28  if (state) {
29  if (_status == IODeviceStatus::BUSY_READ) {
30  doRead();
31  } else if (_status == IODeviceStatus::BUSY_WRITE) {
32  doWrite();
33  }
34  }
35  return state;
36 }
37 
38 void IODeviceStorage::read(ComputerWord* memory, int32_t address) {
39  _status = IODeviceStatus::BUSY_READ;
40  _address = address;
41  _memory = memory;
42  for (int i = 0; i < _blockSize; ++i) {
43  _buffer[i] = _storage[_locator + i];
44  }
46 }
47 
48 void IODeviceStorage::write(const ComputerWord* memory, int32_t address) {
49  _status = IODeviceStatus::BUSY_WRITE;
50  for (int i = 0; i < _blockSize; ++i) {
51  _buffer[i] = memory[address + i];
52  }
54 }
55 
57  for (int i = 0; i < _blockSize; ++i) {
58  _memory[_address + i] = _buffer[i];
59  }
60  _status = IODeviceStatus::READY;
61 }
62 
64  for (int i = 0; i < _blockSize; ++i) {
65  _storage[_locator + i] = _buffer[i];
66  }
67  _status = IODeviceStatus::READY;
68 }
69 
70 IODeviceTape::IODeviceTape(int32_t storageSize) : IODeviceStorage(storageSize) {
71  _type = IODeviceType::TAPE;
72  _readyRate = 0.1;
73 }
74 
75 void IODeviceTape::control(int32_t operation) {
76  if (operation == 0) {
77  _locator = 0;
78  } else {
79  _locator += operation * _blockSize;
80  _locator = std::max(0, _locator);
81  }
82 }
83 
84 void IODeviceTape::doRead() {
86  _locator += _blockSize;
87 }
88 
89 void IODeviceTape::doWrite() {
91  _locator += _blockSize;
92 }
93 
94 IODeviceDisk::IODeviceDisk(int32_t storageSize) : IODeviceStorage(storageSize) {
95  _type = IODeviceType::DISK;
96  _readyRate = 0.5;
97 }
98 
99 void IODeviceDisk::control(int32_t operation) {
100  _locator = operation;
101 }
102 
103 IODeviceSeqReader::IODeviceSeqReader(int32_t storageSize) : IODeviceStorage(storageSize) {
104  _allowWrite = false;
105 }
106 
107 void IODeviceSeqReader::doRead() {
109  _locator += _blockSize;
110 }
111 
112 IODeviceSeqWriter::IODeviceSeqWriter(int32_t storageSize) : IODeviceStorage(storageSize) {
113  _allowRead = false;
114 }
115 
116 void IODeviceSeqWriter::doWrite() {
118  _locator += _blockSize;
119 }
120 
121 IODeviceCardReader::IODeviceCardReader(int32_t storageSize) : IODeviceSeqReader(storageSize) {
122  _type = IODeviceType::CARD_READER;
123  _blockSize = 16;
124  _readyRate = 0.2;
125 }
126 
127 IODeviceCardPunch::IODeviceCardPunch(int32_t storageSize) : IODeviceSeqWriter(storageSize) {
128  _type = IODeviceType::CARD_PUNCH;
129  _blockSize = 16;
130  _readyRate = 0.1;
131 }
132 
133 IODeviceLinePrinter::IODeviceLinePrinter(int32_t storageSize, int32_t pageSize) :
134  IODeviceSeqWriter(storageSize), _pageSize(pageSize) {
135  _type = IODeviceType::LINE_PRINTER;
136  _blockSize = 24;
137  _readyRate = 0.1;
138 }
139 
141  _locator += (_pageSize - _locator / _blockSize % _pageSize) * _blockSize;
142 }
143 
144 int32_t IODeviceLinePrinter::pageOffsetAt(int32_t index) const {
145  return index * _pageSize * _blockSize;
146 }
147 
148 std::string IODeviceLinePrinter::line(int32_t pageNum, int32_t lineNum) const {
149  int32_t offset = pageOffsetAt(pageNum) + lineNum * _blockSize;
150  std::ostringstream out;
151  for (int i = 0; i < _blockSize; ++i) {
152  out << _storage[offset + i].getCharacters();
153  }
154  return out.str();
155 }
156 
157 IODeviceTypewriter::IODeviceTypewriter(int32_t storageSize) : IODeviceSeqReader(storageSize) {
158  _type = IODeviceType::TYPEWRITER;
159  _blockSize = 14;
160  _readyRate = 0.2;
161 }
162 
163 IODevicePaperTape::IODevicePaperTape(int32_t storageSize) : IODeviceSeqReader(storageSize) {
164  _type = IODeviceType::PAPER_TAPE;
165  _blockSize = 14;
166  _readyRate = 0.2;
167 }
168 
170  _locator = 0;
171 }
172 
173 }; // namespace mixal
mixal::IODeviceStorage::doWrite
void doWrite() override
Definition: io.cpp:63
mixal::ComputerWord
Definition: memory.h:25
mixal::IODevice::_allowWrite
bool _allowWrite
Definition: io.h:76
mixal::IODevice::_blockSize
int32_t _blockSize
Definition: io.h:74
mixal::IODeviceStorage
Definition: io.h:87
mixal::IODevice::_timestamp
int32_t _timestamp
Definition: io.h:77
mixal::IODeviceSeqReader
Definition: io.h:136
mixal::IODeviceDisk::control
void control(int32_t operation) final
Definition: io.cpp:99
mixal::IODevice::IODevice
IODevice(int32_t blockSize, bool allowRead, bool allowWrite)
Definition: io.cpp:10
mixal::IODeviceStorage::read
void read(ComputerWord *memory, int32_t address) override
Definition: io.cpp:38
mixal::IODeviceLinePrinter::control
void control(int32_t operation) final
Definition: io.cpp:140
mixal::IODeviceStatus
IODeviceStatus
Definition: io.h:28
mixal::IODeviceType
IODeviceType
Definition: io.h:17
mixal::IODevice::_readyRate
double _readyRate
Definition: io.h:78
mixal::IODevice::ready
virtual bool ready(int32_t elapsed)
Definition: io.cpp:14
mixal::IODeviceLinePrinter::pageOffsetAt
int32_t pageOffsetAt(int32_t index) const
Definition: io.cpp:144
mixal::IODeviceStorage::write
void write(const ComputerWord *memory, int32_t address) override
Definition: io.cpp:48
mixal::IODevicePaperTape::control
void control(int32_t operation) final
Definition: io.cpp:169
mixal::IODevice::_allowRead
bool _allowRead
Definition: io.h:75
mixal::IODeviceStorage::ready
bool ready(int32_t elapsed) override
Definition: io.cpp:26
io.h
IO Devices.
mixal::IODevice::_type
IODeviceType _type
Definition: io.h:73
mixal::IODevice
Definition: io.h:40
mixal::IODeviceLinePrinter::line
std::string line(int32_t pageNum, int32_t lineNum) const
Definition: io.cpp:148
mixal::IODeviceStorage::doRead
void doRead() override
Definition: io.cpp:56
mixal::IODeviceTape::control
void control(int32_t operation) final
Definition: io.cpp:75