*-----------------------------------------------------------
* Test code for 74LS373 latch
* Blinks an LED on D2 1 second on 1 second off at 4MHz clock
* Takes into account that the port is shared with the FDC
*
* WRM 2022-07-23
*
*-----------------------------------------------------------
*

LS373     EQU      $FF2801             ; Write only

*
         ORG $900
FDCSEL   DS.B 1 ; 0x00 to 0x03
FDCSIDE  DS.B 1 ; 0x00 or 0x01
FDCDDEN  DS.B 1 ; 0x00 or 0x01
FDCLED   DS.B 1 ; 0x00 or 0x01
         ORG $1000

* The 74LS373 at $FF2801 is write-only.
*
* Bit 0: DS0
* Bit 1: DS1
* Bit 2: LED to +5V, 0 = LED on
* Bit 3: Pin (unused)
* Bit 4: Pin (unused)
* Bit 5: Pin (unused)	
* Bit 6: Side Select
* Bit 7: 1770 /DDEN
*

START    MOVE.L    #0,FDCSEL           ; make it all zero
LEDON    MOVE.B    #$01,FDCLED
         JSR       FDCSET
         JSR       DELAY1S
LEDOFF   MOVE.B    #0,FDCLED
         JSR       FDCSET
         JSR       DELAY1S
         BRA       LEDON

; https://wiki.neogeodev.org/index.php?title=68k_instructions_timings
; We come here with JSR (18 or 20) or BSR (18)
; 4 MHz, 222220 (18 + 222220 * 18 + 12 -2 + 16 = 4000004)
; Should probably push D0 and account for that in the timing
DELAY1S MOVE.l          #222220,D0     ; 12
DELAYLP SUBQ.l          #1,D0          ; 8
        BNE             DELAYLP        ; 10 if taken, 8 if not
        RTS                            ; 16
; Basically 18n + overhead 

FDCSET   MOVEM     D0-D1,-(SP) ; Push
         MOVE.B    FDCDDEN,D0
         AND.B     #$01,D0
         ASL       #7,D0
         MOVE.B    D0,D1
         MOVE.B    FDCSIDE,D0
         AND.B     #$01,D0
         ASL       #6,D0
         OR.B      D0,D1
         MOVE.B    FDCLED,D0
         AND.B     #$01,D0
         ASL       #2,D0
         OR.B      D0,D1
         MOVE.B    FDCSEL,D0
         AND.B     #$02,D0
         OR.B      D0,D1
         MOVE.B    D1,LS373
         MOVEM     (SP)+,D0-D1
         RTS













