1
0
mirror of https://github.com/rzzzwilson/pymlac.git synced 2025-06-10 09:32:41 +00:00

Problems with state of TTYIN device

This commit is contained in:
Ross Wilson
2015-07-11 18:41:37 +07:00
parent cd0008e416
commit f404fdb46b
2 changed files with 34 additions and 21 deletions

View File

@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python
"""
Emulate the Input TTY device (TTYIN).
@@ -7,7 +7,7 @@ Emulate the Input TTY device (TTYIN).
from Globals import *
import log
log = log.Log('test_CPU.log', log.Log.DEBUG)
log = log.Log('test_TTYIN.log', log.Log.DEBUG)
class TtyIn(object):
@@ -17,11 +17,11 @@ class TtyIn(object):
DEVICE_READY = 1
TTYIN_CHARS_PER_SECOND = 1000
DEVICE_READY_CYCLES = int(CYCLES_PER_SECOND / TTYIN_CHARS_PER_SECOND)
def __init__(self):
"""Initialize the TTYIN device."""
log('TTYIN: Initializing device')
self.filename = None
@@ -32,22 +32,29 @@ class TtyIn(object):
self.status = self.DEVICE_NOT_READY
log('TTYIN: DEVICE_READY_CYCLES=%d' % self.DEVICE_READY_CYCLES)
def mount(self, fname):
"""Mount a file on the TTYIN device."""
log('TTYIN: Mounting file %s on device' % fname)
self.filename = fname
self.open_file = open(fname, 'r')
self.value = 0
self.value = self.open_file.read(1)
self.atEOF = False
self.cycle_count = self.DEVICE_READY_CYCLES
self.status = self.DEVICE_NOT_READY
if len(self.value) < 1:
# EOF on input file
self.atEOF = True
self.cycle_count = 0
self.value = 0
log('TTYIN: EOF set on device (file %s)' % self.fname)
self.value = ord(self.value)
def dismount(self):
"""Dismount the file on the TTYIN device."""
log('TTYIN: Dismounting file %s' % self.filename)
if self.open_file:
@@ -57,43 +64,45 @@ class TtyIn(object):
self.value = 0
self.atEOF = True
self.status = self.DEVICE_NOT_READY
def read(self):
"""Return the current device value."""
log('TTYIN: Reading device, value is %03o' % self.value)
return self.value
def ready(self):
"""Return device status."""
log("TTYIN: Device 'ready' status is %s" % str(self.status == self.DEVICE_READY))
return (self.status == self.DEVICE_READY)
def clear(self):
"""Clear the device 'ready' status."""
log("TTYIN: Clearing device 'ready' status")
self.status = self.DEVICE_NOT_READY
def tick(self, cycles):
"""Advance the device state by 'cycles' number of CPU cycles."""
log("TTYIN: Doing 'tick' with %d cycles, .atEOF is %s" % (cycles, str(self.atEOF)))
if (not self.atEOF):
log('TTYIN: .cycle_count=%d' % self.cycle_count)
if not self.atEOF:
log("TTYIN: Doing 'tick' with %d cycles, .atEOF is %s, .cycle_count=%d"
% (cycles, str(self.atEOF), self.cycle_count))
self.cycle_count -= cycles
if self.cycle_count <= 0:
self.cycle_count = self.DEVICE_READY_CYCLES
log('TTYIN: .cycle_count expired, read new character')
self.cycle_count += self.DEVICE_READY_CYCLES
self.value = self.open_file.read(1)
self.status = self.DEVICE_READY
if len(self.value) < 1:
# EOF on input file
self.atEOF = True
self.value = 0
self.value = chr(0)
self.cycle_count = 0
self.status = self.DEVICE_NOT_READY
log('TTYIN: EOF set on device (file %s)' % self.filename)
self.value = ord(self.value)

View File

@@ -31,6 +31,8 @@ def error(msg):
def create_tty_file(filename):
"""Create a TTY file."""
print('create_tty_file(%s)' % filename)
# create a test file
with open(filename, 'wb') as fd:
# leader
@@ -66,6 +68,8 @@ def mount_dismount(filename):
def read_tty(filename):
"""Mount a file and read from it."""
print('read_tty(%s)' % filename)
ttyin = TtyIn.TtyIn()
ttyin.mount(filename)