Skip to content

Commit

Permalink
added printer class with better buffering
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenjc committed Nov 13, 2024
1 parent 3fc6e3f commit a78416a
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 58 deletions.
73 changes: 73 additions & 0 deletions src/devices/printer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@



# "Q1 ASM IO addresses usage Q1 Lite" p. 75 - 77
class SerialImpactPrinter:

def __init__(self):
self.poshi2 = 0
self.pos = [0.0, 0.0]
self.dir = [0, 0]
self.buffer = ''

def status(self) -> int:
return 0x01 # selected


def output(self, value):
self.buffer += chr(value)


def ctrl_06(self, value: int):
dist = (self.poshi2 << 8) + value
x, y = self.pos
dx, dy = self.dir

if dx != 0: # x-dir
dir = f'<->'
cm = 2.54 * dist / 60 # cm
v = cm * dx
x = x + v
if dx < -0.00001:
print(self.buffer)
self.buffer = ''
else:
self.buffer += ' ' * (int(v/0.21) -1)

if dy != 0: # y-dir
dir = f' I '
cm = 2.54 * dist / 48 # cm
v = cm * dy
y = y + v
self.pos = [x, y]
#print(f'SI printer ctrl: move {dir} {v:6.2f} cm. pos ({x:6.2f}, {y:6.2f})')


def ctrl_07(self, value: int):
self.poshi2 = value & 0x3
desc = ''
if value & 0x80:
desc += 'reset, '
self.pos[0] = 0.0
if value & 0x40:
desc += 'exp res, '
if value & 0x20:
desc += 'raise ribbon, '
if value & 0x10:
desc += 'lower ribbon, '
if value & 0x08:
desc += 'paper '
self.dir = [0, 1]
else:
desc += 'carriage '
self.dir = [1, 0]
if value & 0x04:
self.dir[0] = - self.dir[0]
self.dir[1] = - self.dir[1]
assert self.dir[1] >= 0
desc += 'reverse '
else:
desc += 'forward '
desc += 'motion'

#print(f'SI printer ctrl: - 0x{value:02x} [{desc}]')
65 changes: 13 additions & 52 deletions src/devices/z80io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import devices.disk as disk
import devices.display as display
import devices.printer as printer

#

Expand All @@ -16,11 +17,9 @@ def __init__(self, m, fs):
self.disk1 = disk.Control(1, fs)
self.disk2 = disk.Control(2, fs)
self.display = display.Display()
self.printer = printer.SerialImpactPrinter()

self.prt2bits = 0
self.prtbuf = "" # temporary hack for 'printer' - actually prob not printer
self.prtpos = [0.0, 0.0]
self.prtdir = [0, 0]
self.prtbuf = "" # temporary hack for 'printer'

self.m = m
self.incb = {}
Expand Down Expand Up @@ -181,62 +180,21 @@ def handle_key_out(self, val):
# mention this. See
# "Q1 ASM IO addresses usage Q1 Lite" p. 77
def handle_printer_in_5(self) -> int:
status = 0x01
status = self.printer.status()
self.print(f'IO in - printer 0x5 status - {status} (1 == selected)')
return status

# Print character at current position
def handle_printer_out_5(self, val : int):
print(chr(val))
self.printer.output(val)


# "Q1 ASM IO addresses usage Q1 Lite" p. 75
def handle_printer_out_6(self, val):
dist = (self.prt2bits << 8) + val
x, y = self.prtpos
dx, dy = self.prtdir

if dx != 0: # x-dir
dir = f'<->'
cm = 2.54 * dist / 60 # cm
v = cm * dx
x = x + v
if dy != 0: # y-dir
dir = f' I '
cm = 2.54 * dist / 48 # cm
v = cm * dy
y = y + v
self.prtpos = [x, y]
self.print(f'IO out - printer ctrl 0x6 move {dir} {v:6.2f} cm. pos ({x:6.2f}, {y:6.2f})')
self.printer.ctrl_06(val)


def handle_printer_out_7(self, val):
self.prt2bits = val & 0x3
desc = ''
if val & 0x80:
desc += 'reset, '
self.prtpos[0] = 0.0
if val & 0x40:
desc += 'exp res, '
if val & 0x20:
desc += 'raise ribbon, '
if val & 0x10:
desc += 'lower ribbon, '
if val & 0x08:
desc += 'paper '
self.prtdir = [0, 1]
else:
desc += 'carriage '
self.prtdir = [1, 0]
if val & 0x04:
self.prtdir[0] = - self.prtdir[0]
self.prtdir[1] = - self.prtdir[1]
assert self.prtdir[1] >= 0
desc += 'reverse '
else:
desc += 'forward '
desc += 'motion'

print(f'IO out - printer ctrl 0x7 - 0x{val:02x} [{desc}]')
self.printer.ctrl_07(val)


### Printer 8 - Dot Matrix Printer
Expand Down Expand Up @@ -282,8 +240,11 @@ def handle_disk_in_09(self):

# not disk, possibly printer
def handle_unkn_in_0c(self):
self.print('IO in - unknown in for 0xc - (return 0x00)')
return 0x00
# status 0x80 - program stuck in start up
# status 0x40 - DINDEX stuck in F5
status = 0x00
self.print(f'IO in - unknown in for 0xc - (return {status})')
return status


def handle_unkn_out_0c(self, val):
Expand Down
12 changes: 6 additions & 6 deletions src/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ def kbd_input(self):
kc = self.kc
if self.key.kbhit():
ch = ord(self.key.getch())
if 32 <= ch < 127:
print(f'{chr(ch)}')
else:
print(f'{ch}')
# if 32 <= ch < 127:
# print(f'{chr(ch)}')
# else:
# print(f'{ch}')

if ch == 0x222b: # opt-b -> hexdump
self.cpu.mem.hexdump(0x2000, 0x10000 - 0x2000)
elif ch == 402: # opt-f function keys
k = input("fn key:\n")
print('key:', k)
k = 'F' + input("fn key:\n")
#print('key:', k)
self.int38(kc.okey(k))
elif ch == 8224: # opt-t
args.decode = not args.decode
Expand Down

0 comments on commit a78416a

Please sign in to comment.