Implement the new music in /apple_io.py as well, for running under emulation
authorNick Downing <nick@ndcode.org>
Fri, 20 May 2022 18:08:31 +0000 (04:08 +1000)
committerNick Downing <nick@ndcode.org>
Sun, 22 May 2022 00:21:33 +0000 (10:21 +1000)
apple_io.py

index 2caf314..307abe5 100755 (executable)
@@ -167,6 +167,20 @@ colors = [
   0xf, # white -> intense white
 ]
 
+# see lemonade_tone_patched.lst
+TONE_PERIOD = 0x300
+TONE_DUR = 0x301
+TONE_TONE = 0x302
+TONE_TONE1 = 0x310
+
+# see lemonade_tone_nick.lst
+NICK_REST = 0x300
+NICK_TONE = 0x308
+NICK_DURL = 0x309
+NICK_DURH = 0x30b
+NICK_FREQL = 0x30d
+NICK_FREQH = 0x314
+
 # global state
 attr = None
 fd_in = sys.stdin.fileno()
@@ -180,8 +194,12 @@ mem = {
   ZP_WNDBTM: 24,
   ZP_CH: 0,
   ZP_CV: 0,
-  0x300: 0, # tone period
-  0x301: 0, # tone dur
+  TONE_PERIOD: 0,
+  TONE_DUR: 0,
+  NICK_DURL: 0,
+  NICK_DURH: 0,
+  NICK_FREQL: 0,
+  NICK_FREQH: 0,
 }
 gr_color = 0
 gr_mem = [[0 for j in range(40)] for i in range(40)]
@@ -347,17 +365,37 @@ def poke(addr, data):
 
 def call(addr):
   addr &= 0xffff
-  if addr == 0x302 or addr == 0x310:
+  if addr == NICK_REST or addr == NICK_TONE:
+    # for lemonade_patched, see test/lemonade_tone_nick.py
+    if addr == NICK_REST:
+      mem[NICK_FREQL] = 0
+      mem[NICK_FREQH] = 0
+    frequency_incr = mem[NICK_FREQL] + (mem[NICK_FREQH] << 8)
+    duration_count = -(mem[NICK_DURL] + (mem[NICK_DURH] << 8))
+    duration_count = ((duration_count - 1) & 0xffff) + 1
+    duration = 3.58309497e-10 + duration_count * (
+      2.34821712e-05 + frequency_incr * 4.47591203e-11
+    )
+    if frequency_incr:
+      period = 0x1fffe * (2.34821712e-05 / frequency_incr + 4.47591203e-11)
+      tone(round(1. / period), round(duration * 1e3))
+    else:
+      time.sleep(duration)
+  elif addr == TONE_TONE or addr == TONE_TONE1:
     # for lemonade, see test/lemonade_tone_patched.py
-    a = mem[0x301] # duration_count
-    b = a / (((mem[0x300] - 1) & 0xff) + 1) # duration_count / period_count
-    cycles = 1.37788799e-02 - 4.21513128e-06 * a + 1.27999925e+02 * b
-    duration = 1.27361219e-05 + 2.50702246e-03 * a + 2.50310997e-03 * b
+    period_count = ((mem[TONE_PERIOD] - 1) & 0xff) + 1
+    duration_count = ((mem[TONE_DUR] - 1) & 0xff) + 1
+    cycles = 1.37788799e-02 + duration_count * (
+      -4.21513128e-06 + 1.27999925e+02 / period_count
+    )
+    duration = 1.27361219e-05 + duration_count * (
+      2.50702246e-03 + 2.50310997e-03 / period_count
+    )
     tone(round(cycles / duration), round(duration * 1e3))
-  elif addr == 0x3600:
-    # for lemonade, lighting (setup)
+  elif addr == 0x3600 or addr == 0x95ef:
+    # for lemonade, lighting (init)
     pass
-  elif addr == 0x3603:
+  elif addr == 0x3603 or addr == 0x9586:
     # for lemonade, lightning (execute)
     pass
   elif addr == ROM_CLREOP: