cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

serial.s (2142B)


      1	.include	"global.s"
      2
      3	.globl	.int
      4
      5	.globl	__io_out
      6	.globl	__io_in
      7	.globl	__io_status
      8
      9	.area	_HEADER_SIO (ABS)
     10
     11	.org	0x58		; SIO
     12.int_SIO:
     13	PUSH	AF
     14	PUSH	HL
     15	LD	HL,#.int_0x58
     16	JP	.int
     17
     18	.area	_GSINIT
     19
     20	;; initialize SIO
     21	LD	BC,#.serial_IO
     22	CALL	.add_SIO
     23	
     24	XOR	A
     25	LDH	(.IF),A
     26	
     27	LDH	A,(.IE)
     28	OR	A,#0b00001000	; Serial I/O	=   On
     29	LDH	(.IE),A
     30
     31	LDH	(.SC),A		; Use external clock
     32	LD	A,#.DT_IDLE
     33	LDH	(.SB),A		; Send IDLE byte
     34	LD	A,#0x80
     35	LDH	(.SC),A		; Use external clock
     36
     37	.area	_HOME
     38
     39_add_SIO::
     40	PUSH	BC
     41	LDA	HL,4(SP)	; Skip return address and registers
     42	LD	C,(HL)
     43	INC	HL
     44	LD	B,(HL)
     45	CALL	.add_SIO
     46	POP	BC
     47	RET
     48
     49.add_SIO::
     50	LD	HL,#.int_0x58
     51	JP	.add_int
     52
     53_remove_SIO::
     54	PUSH	BC
     55	LDA	HL,4(SP)	; Skip return address and registers
     56	LD	C,(HL)
     57	INC	HL
     58	LD	B,(HL)
     59	CALL	.remove_SIO
     60	POP	BC
     61	RET
     62
     63.remove_SIO::
     64	LD	HL,#.int_0x58
     65	JP	.remove_int
     66
     67	;; Serial interrupt
     68.serial_IO::
     69	LD	A,(__io_status) ; Get status
     70
     71	CP	#.IO_RECEIVING
     72	JR	NZ, 1$
     73
     74	;; Receiving data
     75	LDH	A,(.SB)		; Get data byte
     76	LD	(__io_in),A	; Store it
     77
     782$:
     79	LD	A,#.IO_IDLE
     803$:
     81	LD	(__io_status),A ; Store status
     82
     83	XOR	A
     84	LDH	(.SC),A		; Use external clock
     85	LD	A,#.DT_IDLE
     86	LDH	(.SB),A		; Reply with IDLE byte
     874$:
     88	LD	A,#0x80
     89	LDH	(.SC),A		; Enable transfer with external clock
     90	RET
     91
     921$:
     93	CP	#.IO_SENDING
     94	JR	NZ, 4$
     95
     96	;; Sending data
     97	LDH	A,(.SB)		; Get data byte
     98	CP	#.DT_RECEIVING
     99	JR	Z, 2$
    100	LD	A,#.IO_ERROR
    101	JR	3$
    102
    103	.area	_DATA
    104
    105__io_out::
    106	.ds	0x01		; Byte to send
    107__io_in::
    108	.ds	0x01		; Received byte
    109__io_status::
    110	.ds	0x01		; Status of serial IO
    111.int_0x58::
    112	.blkw	0x08
    113
    114	.area	_CODE
    115
    116	;; Send byte in __io_out to the serial port
    117.send_byte:
    118_send_byte::			; Banked
    119	LD	A,#.IO_SENDING
    120	LD	(__io_status),A ; Store status
    121	LD	A,#0x01
    122	LDH	(.SC),A		; Use internal clock
    123	LD	A,(__io_out)
    124	LDH	(.SB),A		; Send data byte
    125	LD	A,#0x81
    126	LDH	(.SC),A		; Use internal clock
    127	RET
    128
    129	;; Receive byte from the serial port in __io_in
    130.receive_byte:
    131_receive_byte::			; Banked
    132	LD	A,#.IO_RECEIVING
    133	LD	(__io_status),A ; Store status
    134	XOR	A
    135	LDH	(.SC),A		; Use external clock
    136	LD	A,#.DT_RECEIVING
    137	LDH	(.SB),A		; Send RECEIVING byte
    138	LD	A,#0x80
    139	LDH	(.SC),A		; Use external clock
    140	RET