Implement zero-page emulation for normal/inverse/flash (to help GR emulation)
authorNick Downing <nick@ndcode.org>
Tue, 24 May 2022 02:45:53 +0000 (12:45 +1000)
committerNick Downing <nick@ndcode.org>
Tue, 24 May 2022 02:46:03 +0000 (12:46 +1000)
apple_io.py

index a8059c3..da8144e 100644 (file)
@@ -14,6 +14,10 @@ PCM_PERIODSIZE = 441
 # target will yield this much time whenever it polls HW_IOADR
 POLL_TIMEOUT_MS = 1
 
+INVFLG_NORMAL = 0xff
+INVFLG_INVERSE = 0x3f
+INVFLG_FLASH = 0x7f
+
 # see https://www.tinaja.com/ebooks/tearing_rework.pdf
 ZP_WNDLFT = 0x20 # Left side of scroll window
 ZP_WNDWTH = 0x21 # Width of scroll window
@@ -216,6 +220,7 @@ low_mem[ZP_WNDBTM] = 24
 #low_mem[ZP_CH] = 0
 low_mem[ZP_CV] = 23 # likely value (depends on previous terminal activity)
 #low_mem[ZP_COLOR] = 0
+low_mem[ZP_INVFLG] = INVFLG_NORMAL
 low_mem[ZP_LOMEM] = 4
 low_mem[ZP_LOMEM + 1] = 8
 #low_mem[ZP_HIMEM] = 0
@@ -385,14 +390,29 @@ def home():
   low_mem[ZP_CH] = low_mem[ZP_WNDLFT]
   low_mem[ZP_CV] = low_mem[ZP_WNDTOP]
 
+def invflg():
+  write(
+    '\x1b[0;7;5m'
+  if low_mem[ZP_INVFLG] == INVFLG_FLASH else
+    '\x1b[0;7m'
+  if low_mem[ZP_INVFLG] == INVFLG_INVERSE else
+    '\x1b[0m'
+  )
+
 def normal():
-  write('\x1b[0m')
+  if low_mem[ZP_INVFLG] != INVFLG_NORMAL:
+    low_mem[ZP_INVFLG] = INVFLG_NORMAL
+    invflg()
 
 def inverse():
-  write('\x1b[0;7m')
+  if low_mem[ZP_INVFLG] != INVFLG_INVERSE:
+    low_mem[ZP_INVFLG] = INVFLG_INVERSE
+    invflg()
 
 def flash():
-  write('\x1b[0;7;5m')
+  if low_mem[ZP_INVFLG] != INVFLG_FLASH:
+    low_mem[ZP_INVFLG] = INVFLG_FLASH
+    invflg()
 
 def tone(freq, dur): # Hz, s
   if beep_style == BEEP_STYLE_VT100:
@@ -552,7 +572,7 @@ def gr():
     low_mem[addr:addr + 40] = [0] * 40
 
 def gr_update(x0, y0, x1, y1):
-  write(f'\x1b7') # save all attributes (in case in INVERSE/FLASH mode)
+  write('\x1b[s') # save cursor
   bg = -1
   fg = -1
   br = -1
@@ -592,7 +612,8 @@ def gr_update(x0, y0, x1, y1):
         fg = new_fg
         br = new_br
       write(ch * gr_width)
-  write(f'\x1b8') # restore all attributes (in case in INVERSE/FLASH mode)
+  write('\x1b[u') # restore cursor
+  invflg() # restore normal/inverse/flash
 
 def color(n):
   low_mem[ZP_COLOR] = n