Skip to content

Instantly share code, notes, and snippets.

@anarsoul
Created January 11, 2024 18:02
Show Gist options
  • Save anarsoul/9cea01c43360e5dee96e513c892e3b0d to your computer and use it in GitHub Desktop.
Save anarsoul/9cea01c43360e5dee96e513c892e3b0d to your computer and use it in GitHub Desktop.
commit 388aeb278852e2a861a70c2e4ba5f39f01064571
Author: Vasily Khoruzhick <anarsoul@gmail.com>
Date: Thu Dec 28 23:16:36 2023 -0800
Add support for ay-4bit interface
It's a custom interface and it requires custom ESP8266 firmware
Description for Spectrum-side
bits 0-3: D0-D3, bidirectional
bit 4: CLK, active low, driven by Spectrum
bit 5: DATA_READY (IRQ), active low, driven by ESP
Read byte:
CLK D3 D2 D1 D0
1 x x x x
0 0 1 0 1 <- start word for read, Spectrum drives
D0-D3
1 x x x x <- ESP will start driving D0-D3 here,
but data can be considered
valid only when CLK is 0
0 D3 D2 D1 D0 <- first nibble, ESP drives D0-D3
1 x x x x <- ESP will start driving D0-D3 here,
but data can be considered
valid only when CLK is 0
0 D7 D6 D5 D4 <- second nibble, ESP drives D0-D3
1 x x x x
0 C3 C2 C1 C0 <- Spectrum outputs checksum
1 x x x x
Write byte:
CLK D3 D2 D1 D0
1 x x x x
0 1 0 1 0 <- start word for write, Spectrum drives
D0-D3
1 x x x x
0 D3 D2 D1 D0 <- first nibble, Spectrum drives D0-D3
1 x x x x
0 D7 D6 D5 D4 <- second nibble, Spectrum drives D0-D3
1 x x x x <- ESP will start driving D0-D3 here,
but data can be considered
valid only when CLK is 0
0 C3 C2 C1 C0 <- ESP outputs checksum
1 x x x x
Checksum is D3 ^ D7 << 3 | D2 ^ D6 << 2 | D1 ^ D5 << 1 | D0 ^ D4, i.e.
xor of nibbles
diff --git a/Makefile b/Makefile
index 066aab2..d4258ce 100644
--- a/Makefile
+++ b/Makefile
@@ -31,7 +31,10 @@ zxuno-zxscreen: $(SOURCES)
sjasmplus main.asm -DUNO -DZXSCR --lst=main.lst -DV=$(VERSION)
esxdos-ay: $(SOURCES)
- sjasmplus main.asm -DAY -DZXSCR --lst=main.lst -DV=$(VERSION)
+ sjasmplus main.asm -DAY -DAYCOMMON -DZXSCR --lst=main.lst -DV=$(VERSION)
+
+esxdos-ay4bit: $(SOURCES)
+ sjasmplus main.asm -DAY4BIT -DAYCOMMON -DZXSCR --lst=main.lst -DV=$(VERSION)
emu-timex: $(SOURCES)
sjasmplus main.asm -DEMU -DUNO -DTIMEX --lst=main.lst -DV=$(VERSION)
@@ -43,4 +46,4 @@ emu-zx: $(SOURCES)
sjasmplus main.asm -DEMU -DUNO -DZXSCR --lst=main.lst -DV=$(VERSION)
clean:
- rm $(BINARY) $(LST)
\ No newline at end of file
+ rm $(BINARY) $(LST)
diff --git a/drivers/ay-4bit.asm b/drivers/ay-4bit.asm
new file mode 100644
index 0000000..86b70d9
--- /dev/null
+++ b/drivers/ay-4bit.asm
@@ -0,0 +1,172 @@
+ module Uart
+
+init:
+ ld a, 0x07
+ ld bc, 0xfffd
+ out (c), a
+ ld a, 0xfc
+ ld b, 0xbf
+ out (c), a ; Enable read mode
+
+ ld a, 0x0e
+ ld bc, 0xfffd
+ out (c), a
+
+ ld b, 0xbf
+ ld a, 0b11111111
+ out (c), a
+
+.flush
+ in a, (c)
+ and a, 0b00100000
+ call z, read
+ jr .flush
+
+ ; byte is in a
+write:
+ push af
+ push bc
+ push de
+ di
+ ld bc, 0xfffd
+ ld a, 0x0e
+ out (c), a
+ ld b, 0xbf
+
+ ld e, a
+ ; 0b1010 is start word for write
+ ld a, 0b11111010
+ out (c), a
+ ; latch on falling edge
+ and a, 0b11101111
+ out (c), a
+
+ ld a, e
+ and a, 0b00001111
+ ; checksum
+ ld d, a
+ or a, 0b11110000
+ out (c), a
+ ; latch on falling edge
+ and a, 0b11101111
+ out (c), a
+ ld a, e
+
+ rrca
+ rrca
+ rrca
+ rrca
+
+ and a, 0b00001111
+ ; checksum
+ ld e, a
+ xor d
+ ld d, a
+ ld a, e
+
+ or a, 0b11110000
+ out (c), a
+ ; latch on falling edge
+ and a, 0b11101111
+ out (c), a
+
+ ld a, 0b11111111
+ out (c), a
+ ld a, 0b11101111
+ out (c), a
+
+ in a, (c)
+ and a, 0b00001111
+ cp a, d
+ jr nz, .error
+
+ ld a, 0b11111111
+ out (c), a
+ ei
+ pop de
+ pop bc
+ pop af
+ ret
+
+.error
+ ld a, 0b11111111
+ out (c), a
+ ei
+ pop de
+ pop bc
+ pop af
+ ret
+
+ ; returns byte in a
+read:
+ push bc
+ push de
+ di
+ ld bc, 0xfffd
+ ld a, 0x0e
+ out (c), a
+ ld b, 0xbf
+
+ ; ESP drives IRQ low if it's ready to transmit
+.wait
+ ld a, 0b11111111
+ out (c), a
+ in a, (c)
+ and a, 0b00100000
+ jr nz, .wait
+
+ ; 0b0101 is start word for read
+ ld a, 0b11110101
+ out (c), a
+ ; latch on falling edge
+ and a, 0b11101111
+ out (c), a
+
+ ; receive first nibble
+ ld a, 0b11111111
+ out (c), a
+ ; latch on falling edge
+ ld a, 0b11101111
+ out (c), a
+ in a, (c)
+ and a, 0b00001111
+ ; checksum is in d
+ ld d, a
+
+ ; receive second nibble
+ ld a, 0b11111111
+ out (c), a
+ ; latch on falling edge
+ ld a, 0b11101111
+ out (c), a
+ in a, (c)
+ and a, 0b00001111
+
+ ld e, a
+ rlca
+ rlca
+ rlca
+ rlca
+ ; d now contains received byte
+ or d
+ ld d, a
+ ld a, e
+ ; a now contains checksum in lower 4 bits
+ xor d
+ or a, 0b11110000
+ out (c), a
+ ; latch on falling edge
+ and a, 0b11101111
+ out (c), a
+
+ ld a, 0b11111111
+ out (c), a
+
+ ld a, d
+ pop de
+ pop bc
+
+ ei
+ ret
+
+ endmodule
diff --git a/drivers/index.asm b/drivers/index.asm
index 7bb59be..fc536ff 100644
--- a/drivers/index.asm
+++ b/drivers/index.asm
@@ -9,10 +9,14 @@
IFDEF AY
include "ay-uart.asm"
ENDIF
-
+
+ IFDEF AY4BIT
+ include "ay-4bit.asm"
+ ENDIF
+
include "utils.asm"
include "wifi.asm"
include "proxy.asm"
include "memory.asm"
include "general-sound.asm"
-
\ No newline at end of file
+
diff --git a/gopher/render/ui.asm b/gopher/render/ui.asm
index 266ea92..ff95ac0 100644
--- a/gopher/render/ui.asm
+++ b/gopher/render/ui.asm
@@ -73,4 +73,10 @@ header db " Moon Rabbit "
header db " Moon Rabbit "
db VERSION_STRING
db " for AYWIFI (c) 2021 Alexander Nihirash",13, 0
- ENDIF
\ No newline at end of file
+ ENDIF
+
+ IFDEF AY4BIT
+header db " Moon Rabbit "
+ db VERSION_STRING
+ db " for AY4BIT (c) 2021 Alexander Nihirash",13, 0
+ ENDIF
diff --git a/player/vortex-processor.asm b/player/vortex-processor.asm
index 22196e8..ed622e2 100644
--- a/player/vortex-processor.asm
+++ b/player/vortex-processor.asm
@@ -19,7 +19,7 @@ play:
.stop
call VTPL.MUTE
- IFDEF AY
+ IFDEF AYCOMMON
call restoreAyState
ENDIF
@@ -29,7 +29,7 @@ play:
xor a : ld (Render.play_next), a
jr .stop
- IFDEF AY
+ IFDEF AYCOMMON
restoreAyState:
ld a, #07
ld bc, #fffd
@@ -47,4 +47,4 @@ restoreAyState:
message db "Press key to stop...", 0
ENDMODULE
include "player.asm"
-
\ No newline at end of file
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment