directory structure cleanup

This commit is contained in:
FeralChild64 committed 2019-07-20 22:25:57 +02:00
1 parent c7727d4f0f
commit c30d4b31c9
75 files changed
+45 -45

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
;;
;; Checks whether device numbe (in .A) is a correct IEC device number
;;
;; Preserves .A, .X and .Y
;;
iec_devnum_check:
;; 0, 1, 2, or 3 are illegal - reserved for non-IEC devices on Commodore
cmp #$03
bpl +
bcs +
;; FALLTROUGH
iec_devnum_check_failed:
sec
rts
*
;; 30 and above are illegal too, due to IEC command encoding scheme,
;; see https://www.pagetable.com/?p=1031
cmp #$1E
bpl iec_devnum_check_failed
bcs iec_devnum_check_failed
clc
rts
@@ -0,0 +1,15 @@
iec_pull_atn_clk_release_data:
;; Pulled CLK = DATA considered invalid; for extra robustness on non-emulated
;; hardware give some lines few more cycles to stabilize; see page 10 of
;; http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf
lda CIA2_PRA
ora #BIT_CIA2_PRA_ATN_OUT ; pull
sta CIA2_PRA
ora #BIT_CIA2_PRA_CLK_OUT ; pull
sta CIA2_PRA
and #$FF - BIT_CIA2_PRA_DAT_OUT ; release
sta CIA2_PRA
rts
@@ -0,0 +1,13 @@
iec_pull_clk_release_data:
;; Pulled CLK = DATA considered invalid; for extra robustness on non-emulated
;; hardware give some lines few more cycles to stabilize; see page 10 of
;; http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf
lda CIA2_PRA
ora #BIT_CIA2_PRA_CLK_OUT ; pull
sta CIA2_PRA
and #$FF - BIT_CIA2_PRA_DAT_OUT ; release
sta CIA2_PRA
rts
@@ -0,0 +1,15 @@
iec_pull_data_release_atn_clk:
;; Released CLK = DATA considered valid; for extra robustness on non-emulated
;; hardware give some lines few more cycles to stabilize; see page 10 of
;; http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf
lda CIA2_PRA
ora #BIT_CIA2_PRA_DAT_OUT ; pull
sta CIA2_PRA
and #$FF - BIT_CIA2_PRA_CLK_OUT ; release
sta CIA2_PRA
and #$FF - BIT_CIA2_PRA_ATN_OUT ; release
sta CIA2_PRA
rts
+8
View File
@@ -0,0 +1,8 @@
iec_release_atn:
lda CIA2_PRA
and #$FF - BIT_CIA2_PRA_ATN_OUT ; release
sta CIA2_PRA
rts
+16
View File
@@ -0,0 +1,16 @@
iec_release_atn_clk_data:
;; Released CLK = DATA considered valid; for extra robustness on non-emulated
;; hardware give some lines few more cycles to stabilize; see page 10 of
;; http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf
lda CIA2_PRA
and #$FF - BIT_CIA2_PRA_ATN_OUT ; release
sta CIA2_PRA
and #$FF - BIT_CIA2_PRA_DAT_OUT ; release
sta CIA2_PRA
and #$FF - BIT_CIA2_PRA_CLK_OUT ; release
sta CIA2_PRA
rts
+13
View File
@@ -0,0 +1,13 @@
iec_release_clk_data:
;; Released CLK = DATA considered valid; for extra robustness on non-emulated
;; hardware give some lines few more cycles to stabilize; see page 10 of
;; http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf
lda CIA2_PRA
and #$FF - BIT_CIA2_PRA_DAT_OUT ; release
sta CIA2_PRA
and #$FF - BIT_CIA2_PRA_CLK_OUT ; release
sta CIA2_PRA
rts
@@ -0,0 +1,13 @@
iec_release_clk_pull_data:
;; Released CLK = DATA considered valid; for extra robustness on non-emulated
;; hardware give some lines few more cycles to stabilize; see page 10 of
;; http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf
lda CIA2_PRA
ora #BIT_CIA2_PRA_DAT_OUT ; pull
sta CIA2_PRA
and #$FF - BIT_CIA2_PRA_CLK_OUT ; release
sta CIA2_PRA
rts
+38
View File
@@ -0,0 +1,38 @@
;;
;; Helper routines to return with error/success
;;
;; All of them restore X/Y registers from stack,
;; they take care about Carry flag,
;; error return reeset IEC line to default state,
;; take care about .A and IOSTATUS
;;
iec_return_success:
;; BSOUR is certainly not valid anymore
lda #$00
sta C3PO
;; Restore registers
pla
tay
pla
tax
;; Report success
clc
rts
iec_return_DEVICE_NOT_FOUND:
;; Set IEC line to normal idle state
jsr iec_set_idle
;; BSOUR is certainly not valid anymore
lda #$00
sta C3PO
;; Restore registers
pla
tay
pla
tax
;; Report error
jmp kernalerror_DEVICE_NOT_FOUND
+123
View File
@@ -0,0 +1,123 @@
;; Receive a byte from the IEC bus.
;; Implemented based on https://www.pagetable.com/?p=1135, https://github.com/mist64/cbmbus_doc,
;; http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf (page 11)
iec_rx_byte:
;; Timing is critical here - execute on disabled IRQs
php
sei
;; First, we wait for the talker to release the CLK line
jsr iec_wait_for_clk_release
;; We then release the DATA to signal we are ready
;; We can use this routine, since we weren't pulling CLK anyway
jsr iec_release_clk_data
;; Wait till data line is released (someone else might be holding it)
jsr iec_wait_for_data_release
;; Wait for talker to pull CLK.
;; If over 200 usec (205 cycles on NTSC machine) , then it is EOI.
;; Loop iteraction takes 13 cycles, 17 full iterations are enough
ldx #$11 ; 2 cycles
iec_rx_clk_wait1:
lda CIA2_PRA ; 4 cycles
rol ; 2 cycles, to put CLK in as the last bit
bpl iec_rx_not_eoi ; 2 cycles if not jumped
dex ; 2 cycles
bne iec_rx_clk_wait1 ; 3 cycles if jumped
;; Timeout - either this is the last byte of stream, or the stream is empty at all.
;; Mark end of stream in IOSTATUS
jsr kernalstatus_EOI
iec_rx_not_empty:
;; Pull data for 60 usec to confirm it
jsr iec_release_clk_pull_data
jsr iec_wait60us
jsr iec_release_clk_data
iec_rx_not_eoi:
;; Latch input bits in on rising edge of CLK, eight times for eight bits.
ldx #7
;; Get empty byte to load into.
lda #$00
pha
iec_rx_bit_loop:
;; Wait for CLK to release
jsr iec_wait_for_clk_release
;; (we do this implicitly below, with a tighter routine,
;; so that we don't have timing problems, as the requirements
;; are quite tight. Basically we need to read the clock and data
;; bit from the same byte read.
;; DATA now has the next bit, but inverted (well, except that it turns out not to be).
;; DATA is in bit 7, which is a bit annoying.
;; But we can clock it out with a ROL instruction
;; so that it is in C. We can then ROR it into the
;; partial data byte.
;; We use ROR so that we shift in from the top, so that
;; the first bit we shift in ends up in bit 0 after all
;; 8 bits have been read.
;; ODD: For some reason we don't need to invert the
;; received bits. This is weird, because we invert them
;; on the way out, and everything in the protocol seems
;; to indicate that we sould do so. But experimentation
;; has confirmed the bits don't need inversion on reception.
;; Move data bit into C flag, and loop until bit 6 clears
;; i.e., the clock has been released.
* lda CIA2_PRA
rol
bpl -
;; Pull it into the data byte
pla
ror
pha
;; Wait for CLK to be pulled again, over 300 usec (307 cycles on NTSC)
;; consider this a timeout
ldy #$19 ; 2 cycles
iec_rx_clk_wait2:
lda CIA2_PRA ; 4 cycles
rol ; 2 cycles, to put CLK in as the last bit
bpl + ; 2 cycles if not jumped
dey ; 2 cycles
bne iec_rx_clk_wait2 ; 3 cycles if jumped
;; Timeout
jsr kernalstatus_EOI
pla
plp
sec ; XXX confirm that sec is really a way to report timeout here
rts
*
;; More bits?
dex
bpl iec_rx_bit_loop
;; Then we must within 1000 usec acknowledge the frame by
;; pulling DATA. At this point, CLK is pulled by the
;; talker and DATA by us, i.e., we are ready to receive
;; the next byte. (p11).
jsr iec_release_clk_pull_data
;; Retreive the received byte
pla
;; Return no-error
plp
clc
rts
+7
View File
@@ -0,0 +1,7 @@
iec_set_idle:
lda CIA2_PRA
ora #BIT_CIA2_PRA_CLK_OUT ; pull
and #$FF - BIT_CIA2_PRA_DAT_OUT - BIT_CIA2_PRA_ATN_OUT ; release
sta CIA2_PRA
rts
+33
View File
@@ -0,0 +1,33 @@
;; According to http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf
;; pages 7--8.
;; See also https://www.pagetable.com/?p=1135, https://github.com/mist64/cbmbus_doc
iec_turnaround_to_listen:
;; Timing is critical here - execute on disabled IRQs
php
sei
;; Pull DATA, release CLK and ATN (we are not sending commands)
jsr iec_pull_data_release_atn_clk
;; Wait for clock line to be pulled by the drive
ldx #$FF ; XXX should we really use that high number?
*
lda CIA2_PRA
rol ; to put BIT_CIA2_PRA_CLK_IN as the last (sign) bit
bpl +
dex
bne -
;; Timeout
plp
sec
rts
;; CLK is pulled by the device, OK
*
plp
clc
rts
+23
View File
@@ -0,0 +1,23 @@
;; According to http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf
;; pages 7--8.
;; See also https://www.pagetable.com/?p=1135, https://github.com/mist64/cbmbus_doc
iec_turnaround_to_talk:
;; Timing is critical here - execute on disabled IRQs
php
sei
;; First make sure both CLK, DATA and ATN are released at our side
jsr iec_release_atn
jsr iec_release_clk_data
;; Wait till device releases CLK line
jsr iec_wait_for_clk_release
;; Pull CLK line, it should be held by talker
jsr iec_pull_clk_release_data
plp
clc
rts
+38
View File
@@ -0,0 +1,38 @@
;; Implemented based on https://www.pagetable.com/?p=1135, https://github.com/mist64/cbmbus_doc
;; Expects byte to send in BSOUR; Carry flag set = signal EOI
;; Preserves .X and .Y registers
iec_tx_byte:
;; Store .X and .Y on the stack - preserve them
txa
pha
tya
pha
;; Notify all devices that we are going to send a byte
;; and it is going to be a data byte (released ATN)
jsr iec_release_atn_clk_data
;; Common part of iec_txbyte and iec_tx_common - waits for devices
;; and transmits a byte
jsr iec_tx_common
;; Give device time to tell if they are busy by pulling DATA
;; They should do it within 1ms
ldx #$FF
* lda CIA2_PRA
;; BPL here is checking that bit 7 clears,
;; i.e, that the DATA line is pulled by drive
bpl +
dex
bne -
bpl iec_return_DEVICE_NOT_FOUND
*
jmp iec_return_success
+66
View File
@@ -0,0 +1,66 @@
;; Implemented based on https://www.pagetable.com/?p=1135, https://github.com/mist64/cbmbus_doc
;; Expects command to send in BSOUR; Carry flag set = signal EOI
;; Preserves .X and .Y registers
iec_tx_command:
;; Store .X and .Y on the stack - preserve them
txa
pha
tya
pha
;; Notify all devices that we are going to send a byte
;; and it is going to be a command (pulled ATN)
jsr iec_pull_atn_clk_release_data
;; Give devices time to respond (response is mandatory!)
jsr wait1ms
;; Did at least one device respond by pulling DATA?
lda CIA2_PRA
and #BIT_CIA2_PRA_DAT_IN ; XXX try to optimize this, move to separate routine
beq +
;; No devices present on the bus, so we can immediately return with device not found
jmp iec_return_DEVICE_NOT_FOUND
*
;; At least one device responded, but they are still allowed to stall
;; (can be busy processing something), we have to wait till they are all
;; ready (or bored with DOS attack...)
;; Release back CLK, keep DATA released - to ask devices for status.
jsr iec_release_clk_data
;; Common part of iec_txbyte and iec_tx_common - waits for devices
;; and transmits a byte
clc ; Carry flag set is used for EOI mark
jsr iec_tx_common
;; According to https://www.pagetable.com/?p=1135 there is some complicated and dangerous
;; flow here, but http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf (page 6)
;; advices to just wait 1ms and check the DATA status
jsr wait1ms
lda CIA2_PRA
and #BIT_CIA2_PRA_DAT_IN ; XXX try to optimize this, move to separate routine
beq +
;; XXX possible optimization of the flow above: for many commands it is enough
;; to wait for the DATA being pulled, as confirmation is expected from
;; a single device only - but not sure if it's worth the trouble
jmp iec_return_DEVICE_NOT_FOUND
*
;; All done
;; XXX All the documentation says we should release ATN at this point, but when I do so
;; the turnaround mechanism does not work. Why???
;; jsr iec_wait20us
;; jsr iec_set_idle
jmp iec_return_success
+73
View File
@@ -0,0 +1,73 @@
;; This is a common part of iec_tx_byte and iec_tx_command
;; Implemented based on https://www.pagetable.com/?p=1135, https://github.com/mist64/cbmbus_doc
;; and http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf
;; Expects byte to send in BSOUR; Carry flag set = signal EOI
iec_tx_common:
;; Timing is critical here - execute on disabled IRQs
php
sei
;; Wait till all receivers are ready, they should all release DATA
jsr iec_wait_for_data_release
;; At this point a delay 256 usec or more is considered EOI - add waits if necessary
bcc +
jsr iec_wait100us
jsr iec_wait100us
jsr iec_wait60us
;; XXX waits above should be enough - why do we need more waits???
jsr iec_wait100us
jsr iec_wait100us
jsr iec_wait100us
;; Receiver should now acknowledge EOI by pulling data for at least 60 usec
jsr iec_wait60us
jsr iec_wait_for_data_release
*
;; Pull CLK back to indicate that DATA is not valid, keep it for 60us
;; We can use this routine as we don't hold DATA anyway (and its state doesn't even matter)
jsr iec_pull_clk_release_data
jsr iec_wait60us
;; Now, we can start transmission of 8 bits of data
ldx #7
iec_tx_common_sendbit:
;; Is next bit 0 or 1?
lda BSOUR
lsr
sta BSOUR
bcs +
;; XXX we are actually destroying BSOUR here... is it allowed?
;; Bit is 0
jsr iec_release_clk_pull_data
jmp iec_tx_common_bit_is_sent
*
;; Bit is 1
jsr iec_release_clk_data
iec_tx_common_bit_is_sent:
;; Wait 20us, so that device(s) can pick DATA
jsr iec_wait20us
;; Pull CLK for 20us again, before sending the next bit
;; or performing any other action
jsr iec_pull_clk_release_data
jsr iec_wait20us ; XXX this might be an overkill, there is a lot of code besides this wait here - few NOPs might be enough
;; More bits to send?
dex
bpl iec_tx_common_sendbit
plp
rts ; Flow continues differently for data/command
+22
View File
@@ -0,0 +1,22 @@
iec_wait100us:
;; Wait 100usec
;; PAL:
;; - CPU frequency 0.985248 MHz, we need to waste at least 99 cycles
;; NTSC:
;; - CPU frequency 1.022727 MHZ, we need to waste at least 103 cycles
;; Waste cycles in a loop
ldy #$12 ; 2 cycles
* dey ; 2 cycles * 18
bpl - ; 3 cycles * 17 + 2 cycles
rts ; 6 cycles
;; 6 cycles JSR to routine,
;; 97 cycles routine with RTS
;; 1 cycle to fetch next instruction
;; ---
;; 104 cycles
+22
View File
@@ -0,0 +1,22 @@
iec_wait20us:
;; Wait 20usec
;; PAL:
;; - CPU frequency 0.985248 MHz, we need to waste at least 20 cycles
;; NTSC:
;; - CPU frequency 1.022727 MHZ, we need to waste at least 21 cycles
;; Waste cycles by reading some unimportant memory location
;; in order not to clutter VICE debug logs
ora $03FF ; 4 cycles
ora $03FF ; 4 cycles
rts ; 6 cycles
;; 6 cycles JSR to routine,
;; 14 cycles routine with RTS
;; 1 cycle to fetch next instruction
;; ---
;; 21 cycles
+23
View File
@@ -0,0 +1,23 @@
iec_wait60us:
;; Wait 60usec
;; PAL:
;; - CPU frequency 0.985248 MHz, we need to waste at least 60 cycles
;; NTSC:
;; - CPU frequency 1.022727 MHZ, we need to waste at least 62 cycles
;; Waste cycles in a loop
ldy #$0A ; 2 cycles
* dey ; 2 cycles * 10
bpl - ; 3 cycles * 9 + 2 cycles
rts ; 6 cycles
;; 6 cycles JSR to routine,
;; 57 cycles routine with RTS
;; 1 cycle to fetch next instruction
;; ---
;; 64 cycles
@@ -0,0 +1,7 @@
iec_wait_for_clk_release:
lda CIA2_PRA
and #BIT_CIA2_PRA_CLK_IN
beq iec_wait_for_clk_release
rts
+8
View File
@@ -0,0 +1,8 @@
iec_wait_for_data_pull:
lda CIA2_PRA
;; Check the highest bit, which is DATA IN,
;; (highest bit set = negative value)
bmi iec_wait_for_data_pull
rts
@@ -0,0 +1,8 @@
iec_wait_for_data_release:
lda CIA2_PRA
;; Check the highest bit, which is DATA IN,
;; (highest bit set = negative value)
bpl iec_wait_for_data_release
rts