;
; ROM code for basic play-about Z80 machine
;
; Basic machine memory map:
;
; 0000 - 3fff	ROM
; 4000 - df00	Work RAM (initial SP=0xdfe0)
; e000 - ffff	SCREEN	(256x256 monochrome, flat mapped)
;
; Special addresses:
; dff0 (word)	Vector to jump to on IRQ
; dff2 (word)	Vector to jump to on NMI
;
; Ports:
;
; FE00		Key pressed (codes match keycodes.h).  Cleared to zero after
;		read.
; FE01		Key pressed (codes match keycodes.h).  Cleared when key
;		released.
;
; Notes:
;
; 1.	Interrupts are disabled by default.
; 2.	Interrupt users should just RET from handlers installed to the vectors.
;	Note that the alternate register set is switched to during the
;	interrupt.
;
#define INITSP	$dfe0
#define SCRADDR	$e000
#define SCRW	32
#define SCRH	256
#define IRQVEC	$dff0
#define NMIVEC	$dff2
#define CHARPTR	$3600
#define CHARMAP	$3800


	.org $0

	; Jump to startup
	;
	jp	ROM_START

	; Interrupt handlers
	;
	.org	$38
	ex	af,af'
	exx
	ld	hl,(IRQVEC)
	call	CALL_HL
	ex	af,af'
	exx

	.org	$66
	ex	af,af'
	exx
	ld	hl,(IRQVEC)
	call	CALL_HL
	ex	af,af'
	exx
	reti


	; ROM routine jumpblock
	;
	.org $100
	jp	PRINT
	jp	PRINT_CHAR
	;jp	CLS
	;jp	INPUT
	;jp	WAIT
	jp	$0

	; Start of routines
	;
	.org $200

	; ROM_START
	;
	; Warm start machine.  Waits until 0x4000 contains something other than
	; zero and then executes it.
	;
ROM_START
	di

	ld	sp,$dff0
	ld	hl,INTDEF
	ld	(IRQVEC),hl
	ld	(NMIVEC),hl

	ld	de,0
	ld	hl,signon
	call	PRINT

wait
	ld	a,($4000)
	or	a
	jr	z,wait
	call	$4000
	jp	$

signon
	.byte "bb80-rom.asm $Revision: 0.1 $",0


INTDEF
	.MODULE INTDEF
	;
	; Default interrupt vectors
	;
	ret


CALL_HL
	.MODULE CALL_HL
	;
	; Emulates a call (hl) instructions
	;
	jp	(hl)


PRINT
	.MODULE PRINT
	;
	; E	-	X Co-ord
	; D	-	Y Co-ord
	; HL	-	Pointer to zero terminated string of ASCII chars
	;
loop
	ld	a,(hl)
	or	a
	ret	z
	call	PRINT_CHAR
	inc	hl
	inc	de
	jr	loop

PRINT_CHAR
	.MODULE PRINT_CHAR
	;
	; E	-	X Co-ord
	; D	-	Y Co-ord
	; A	-	Char to print
	;

	push	bc
	push	de
	push	hl
	push	ix

	ld	hl,CHARPTR
	ld	b,0
	ld	c,a
	add	hl,bc
	add	hl,bc

	ld	c,(hl)
	inc	hl
	ld	b,(hl)
	push	bc
	pop	ix

	ld	hl,SCRADDR
	ld	a,d
	ld	d,0
	add	hl,de
	ld	de,SCRW*8

findaddr
	or	a
	jr	z,foundaddr
	add	hl,de
	dec	a
	jr	findaddr

foundaddr
	ld	b,8
	ld	de,SCRW
doprint
	ld	a,(ix)
	ld	(hl),a
	inc	ix
	add	hl,de
	jr	c,exit
	djnz	doprint

exit
	pop	ix
	pop	hl
	pop	de
	pop	bc
	ret

	.MODULE CHAR_MAPS

	; Character pointers
	;
	.org CHARPTR
	.word CHARMAP+0
	.word CHARMAP+8
	.word CHARMAP+16
	.word CHARMAP+24
	.word CHARMAP+32
	.word CHARMAP+40
	.word CHARMAP+48
	.word CHARMAP+56
	.word CHARMAP+64
	.word CHARMAP+72
	.word CHARMAP+80
	.word CHARMAP+88
	.word CHARMAP+96
	.word CHARMAP+104
	.word CHARMAP+112
	.word CHARMAP+120
	.word CHARMAP+128
	.word CHARMAP+136
	.word CHARMAP+144
	.word CHARMAP+152
	.word CHARMAP+160
	.word CHARMAP+168
	.word CHARMAP+176
	.word CHARMAP+184
	.word CHARMAP+192
	.word CHARMAP+200
	.word CHARMAP+208
	.word CHARMAP+216
	.word CHARMAP+224
	.word CHARMAP+232
	.word CHARMAP+240
	.word CHARMAP+248
	.word CHARMAP+256
	.word CHARMAP+264
	.word CHARMAP+272
	.word CHARMAP+280
	.word CHARMAP+288
	.word CHARMAP+296
	.word CHARMAP+304
	.word CHARMAP+312
	.word CHARMAP+320
	.word CHARMAP+328
	.word CHARMAP+336
	.word CHARMAP+344
	.word CHARMAP+352
	.word CHARMAP+360
	.word CHARMAP+368
	.word CHARMAP+376
	.word CHARMAP+384
	.word CHARMAP+392
	.word CHARMAP+400
	.word CHARMAP+408
	.word CHARMAP+416
	.word CHARMAP+424
	.word CHARMAP+432
	.word CHARMAP+440
	.word CHARMAP+448
	.word CHARMAP+456
	.word CHARMAP+464
	.word CHARMAP+472
	.word CHARMAP+480
	.word CHARMAP+488
	.word CHARMAP+496
	.word CHARMAP+504
	.word CHARMAP+512
	.word CHARMAP+520
	.word CHARMAP+528
	.word CHARMAP+536
	.word CHARMAP+544
	.word CHARMAP+552
	.word CHARMAP+560
	.word CHARMAP+568
	.word CHARMAP+576
	.word CHARMAP+584
	.word CHARMAP+592
	.word CHARMAP+600
	.word CHARMAP+608
	.word CHARMAP+616
	.word CHARMAP+624
	.word CHARMAP+632
	.word CHARMAP+640
	.word CHARMAP+648
	.word CHARMAP+656
	.word CHARMAP+664
	.word CHARMAP+672
	.word CHARMAP+680
	.word CHARMAP+688
	.word CHARMAP+696
	.word CHARMAP+704
	.word CHARMAP+712
	.word CHARMAP+720
	.word CHARMAP+728
	.word CHARMAP+736
	.word CHARMAP+744
	.word CHARMAP+752
	.word CHARMAP+760
	.word CHARMAP+768
	.word CHARMAP+776
	.word CHARMAP+784
	.word CHARMAP+792
	.word CHARMAP+800
	.word CHARMAP+808
	.word CHARMAP+816
	.word CHARMAP+824
	.word CHARMAP+832
	.word CHARMAP+840
	.word CHARMAP+848
	.word CHARMAP+856
	.word CHARMAP+864
	.word CHARMAP+872
	.word CHARMAP+880
	.word CHARMAP+888
	.word CHARMAP+896
	.word CHARMAP+904
	.word CHARMAP+912
	.word CHARMAP+920
	.word CHARMAP+928
	.word CHARMAP+936
	.word CHARMAP+944
	.word CHARMAP+952
	.word CHARMAP+960
	.word CHARMAP+968
	.word CHARMAP+976
	.word CHARMAP+984
	.word CHARMAP+992
	.word CHARMAP+1000
	.word CHARMAP+1008
	.word CHARMAP+1016
	.word CHARMAP+1024
	.word CHARMAP+1032
	.word CHARMAP+1040
	.word CHARMAP+1048
	.word CHARMAP+1056
	.word CHARMAP+1064
	.word CHARMAP+1072
	.word CHARMAP+1080
	.word CHARMAP+1088
	.word CHARMAP+1096
	.word CHARMAP+1104
	.word CHARMAP+1112
	.word CHARMAP+1120
	.word CHARMAP+1128
	.word CHARMAP+1136
	.word CHARMAP+1144
	.word CHARMAP+1152
	.word CHARMAP+1160
	.word CHARMAP+1168
	.word CHARMAP+1176
	.word CHARMAP+1184
	.word CHARMAP+1192
	.word CHARMAP+1200
	.word CHARMAP+1208
	.word CHARMAP+1216
	.word CHARMAP+1224
	.word CHARMAP+1232
	.word CHARMAP+1240
	.word CHARMAP+1248
	.word CHARMAP+1256
	.word CHARMAP+1264
	.word CHARMAP+1272
	.word CHARMAP+1280
	.word CHARMAP+1288
	.word CHARMAP+1296
	.word CHARMAP+1304
	.word CHARMAP+1312
	.word CHARMAP+1320
	.word CHARMAP+1328
	.word CHARMAP+1336
	.word CHARMAP+1344
	.word CHARMAP+1352
	.word CHARMAP+1360
	.word CHARMAP+1368
	.word CHARMAP+1376
	.word CHARMAP+1384
	.word CHARMAP+1392
	.word CHARMAP+1400
	.word CHARMAP+1408
	.word CHARMAP+1416
	.word CHARMAP+1424
	.word CHARMAP+1432
	.word CHARMAP+1440
	.word CHARMAP+1448
	.word CHARMAP+1456
	.word CHARMAP+1464
	.word CHARMAP+1472
	.word CHARMAP+1480
	.word CHARMAP+1488
	.word CHARMAP+1496
	.word CHARMAP+1504
	.word CHARMAP+1512
	.word CHARMAP+1520
	.word CHARMAP+1528
	.word CHARMAP+1536
	.word CHARMAP+1544
	.word CHARMAP+1552
	.word CHARMAP+1560
	.word CHARMAP+1568
	.word CHARMAP+1576
	.word CHARMAP+1584
	.word CHARMAP+1592
	.word CHARMAP+1600
	.word CHARMAP+1608
	.word CHARMAP+1616
	.word CHARMAP+1624
	.word CHARMAP+1632
	.word CHARMAP+1640
	.word CHARMAP+1648
	.word CHARMAP+1656
	.word CHARMAP+1664
	.word CHARMAP+1672
	.word CHARMAP+1680
	.word CHARMAP+1688
	.word CHARMAP+1696
	.word CHARMAP+1704
	.word CHARMAP+1712
	.word CHARMAP+1720
	.word CHARMAP+1728
	.word CHARMAP+1736
	.word CHARMAP+1744
	.word CHARMAP+1752
	.word CHARMAP+1760
	.word CHARMAP+1768
	.word CHARMAP+1776
	.word CHARMAP+1784
	.word CHARMAP+1792
	.word CHARMAP+1800
	.word CHARMAP+1808
	.word CHARMAP+1816
	.word CHARMAP+1824
	.word CHARMAP+1832
	.word CHARMAP+1840
	.word CHARMAP+1848
	.word CHARMAP+1856
	.word CHARMAP+1864
	.word CHARMAP+1872
	.word CHARMAP+1880
	.word CHARMAP+1888
	.word CHARMAP+1896
	.word CHARMAP+1904
	.word CHARMAP+1912
	.word CHARMAP+1920
	.word CHARMAP+1928
	.word CHARMAP+1936
	.word CHARMAP+1944
	.word CHARMAP+1952
	.word CHARMAP+1960
	.word CHARMAP+1968
	.word CHARMAP+1976
	.word CHARMAP+1984
	.word CHARMAP+1992
	.word CHARMAP+2000
	.word CHARMAP+2008
	.word CHARMAP+2016
	.word CHARMAP+2024
	.word CHARMAP+2032
	.word CHARMAP+2040

	; Actual Character map
	;
	.org CHARMAP
	.byte 255,195,195,195,195,195,195,255
	.byte 255,192,192,192,192,192,192,192
	.byte 24,24,24,24,24,24,24,255
	.byte 3,3,3,3,3,3,3,255
	.byte 12,24,48,126,12,24,48,0
	.byte 255,195,231,219,219,231,195,255
	.byte 0,1,3,6,204,120,48,0
	.byte 60,102,195,195,255,36,231,0
	.byte 0,0,48,96,255,96,48,0
	.byte 0,0,12,6,255,6,12,0
	.byte 24,24,24,24,219,126,60,24
	.byte 24,60,126,219,24,24,24,24
	.byte 24,90,60,153,219,126,60,24
	.byte 0,3,51,99,254,96,48,0
	.byte 60,102,255,219,219,255,102,60
	.byte 60,102,195,219,219,195,102,60
	.byte 255,195,195,255,195,195,195,255
	.byte 60,126,219,219,223,195,102,60
	.byte 60,102,195,223,219,219,126,60
	.byte 60,102,195,251,219,219,126,60
	.byte 60,126,219,219,251,195,102,60
	.byte 0,1,51,30,206,123,49,0
	.byte 126,102,102,102,102,102,102,231
	.byte 3,3,3,255,3,3,3,0
	.byte 255,102,60,24,24,60,102,255
	.byte 24,24,60,60,60,60,24,24
	.byte 60,102,102,48,24,0,24,0
	.byte 60,102,195,255,195,195,102,60
	.byte 255,219,219,219,251,195,195,255
	.byte 255,195,195,251,219,219,219,255
	.byte 255,195,195,223,219,219,219,255
	.byte 255,219,219,219,223,195,195,255
	.byte 0,0,0,0,0,0,0,0
	.byte 24,24,24,24,24,0,24,0
	.byte 108,108,108,0,0,0,0,0
	.byte 108,108,254,108,254,108,108,0
	.byte 24,62,88,60,26,124,24,0
	.byte 0,198,204,24,48,102,198,0
	.byte 56,108,56,118,220,204,118,0
	.byte 24,24,48,0,0,0,0,0
	.byte 12,24,48,48,48,24,12,0
	.byte 48,24,12,12,12,24,48,0
	.byte 0,102,60,255,60,102,0,0
	.byte 0,24,24,126,24,24,0,0
	.byte 0,0,0,0,0,24,24,48
	.byte 0,0,0,126,0,0,0,0
	.byte 0,0,0,0,0,24,24,0
	.byte 6,12,24,48,96,192,128,0
	.byte 124,198,206,214,230,198,124,0
	.byte 24,56,24,24,24,24,126,0
	.byte 60,102,6,60,96,102,126,0
	.byte 60,102,6,28,6,102,60,0
	.byte 28,60,108,204,254,12,30,0
	.byte 126,98,96,124,6,102,60,0
	.byte 60,102,96,124,102,102,60,0
	.byte 126,102,6,12,24,24,24,0
	.byte 60,102,102,60,102,102,60,0
	.byte 60,102,102,62,6,102,60,0
	.byte 0,0,24,24,0,24,24,0
	.byte 0,0,24,24,0,24,24,48
	.byte 12,24,48,96,48,24,12,0
	.byte 0,0,126,0,0,126,0,0
	.byte 96,48,24,12,24,48,96,0
	.byte 60,102,102,12,24,0,24,0
	.byte 124,198,222,222,222,192,124,0
	.byte 24,60,102,102,126,102,102,0
	.byte 252,102,102,124,102,102,252,0
	.byte 60,102,192,192,192,102,60,0
	.byte 248,108,102,102,102,108,248,0
	.byte 254,98,104,120,104,98,254,0
	.byte 254,98,104,120,104,96,240,0
	.byte 60,102,192,192,206,102,62,0
	.byte 102,102,102,126,102,102,102,0
	.byte 126,24,24,24,24,24,126,0
	.byte 30,12,12,12,204,204,120,0
	.byte 230,102,108,120,108,102,230,0
	.byte 240,96,96,96,98,102,254,0
	.byte 198,238,254,254,214,198,198,0
	.byte 198,230,246,222,206,198,198,0
	.byte 56,108,198,198,198,108,56,0
	.byte 252,102,102,124,96,96,240,0
	.byte 56,108,198,198,218,204,118,0
	.byte 252,102,102,124,108,102,230,0
	.byte 60,102,96,60,6,102,60,0
	.byte 126,90,24,24,24,24,60,0
	.byte 102,102,102,102,102,102,60,0
	.byte 102,102,102,102,102,60,24,0
	.byte 198,198,198,214,254,238,198,0
	.byte 198,108,56,56,108,198,198,0
	.byte 102,102,102,60,24,24,60,0
	.byte 254,198,140,24,50,102,254,0
	.byte 60,48,48,48,48,48,60,0
	.byte 192,96,48,24,12,6,2,0
	.byte 60,12,12,12,12,12,60,0
	.byte 24,60,126,24,24,24,24,0
	.byte 0,0,0,0,0,0,0,255
	.byte 48,24,12,0,0,0,0,0
	.byte 0,0,120,12,124,204,118,0
	.byte 224,96,124,102,102,102,220,0
	.byte 0,0,60,102,96,102,60,0
	.byte 28,12,124,204,204,204,118,0
	.byte 0,0,60,102,126,96,60,0
	.byte 28,54,48,120,48,48,120,0
	.byte 0,0,62,102,102,62,6,124
	.byte 224,96,108,118,102,102,230,0
	.byte 24,0,56,24,24,24,60,0
	.byte 6,0,14,6,6,102,102,60
	.byte 224,96,102,108,120,108,230,0
	.byte 56,24,24,24,24,24,60,0
	.byte 0,0,108,254,214,214,198,0
	.byte 0,0,220,102,102,102,102,0
	.byte 0,0,60,102,102,102,60,0
	.byte 0,0,220,102,102,124,96,240
	.byte 0,0,118,204,204,124,12,30
	.byte 0,0,220,118,96,96,240,0
	.byte 0,0,60,96,60,6,124,0
	.byte 48,48,124,48,48,54,28,0
	.byte 0,0,102,102,102,102,62,0
	.byte 0,0,102,102,102,60,24,0
	.byte 0,0,198,214,214,254,108,0
	.byte 0,0,198,108,56,108,198,0
	.byte 0,0,102,102,102,62,6,124
	.byte 0,0,126,76,24,50,126,0
	.byte 14,24,24,112,24,24,14,0
	.byte 24,24,24,24,24,24,24,0
	.byte 112,24,24,14,24,24,112,0
	.byte 118,220,0,0,0,0,0,0
	.byte 204,51,204,51,204,51,204,51
	.byte 0,0,0,0,0,0,0,0
	.byte 240,240,240,240,0,0,0,0
	.byte 15,15,15,15,0,0,0,0
	.byte 255,255,255,255,0,0,0,0
	.byte 0,0,0,0,240,240,240,240
	.byte 240,240,240,240,240,240,240,240
	.byte 15,15,15,15,240,240,240,240
	.byte 255,255,255,255,240,240,240,240
	.byte 0,0,0,0,15,15,15,15
	.byte 240,240,240,240,15,15,15,15
	.byte 15,15,15,15,15,15,15,15
	.byte 255,255,255,255,15,15,15,15
	.byte 0,0,0,0,255,255,255,255
	.byte 240,240,240,240,255,255,255,255
	.byte 15,15,15,15,255,255,255,255
	.byte 255,255,255,255,255,255,255,255
	.byte 0,0,0,24,24,0,0,0
	.byte 24,24,24,24,24,0,0,0
	.byte 0,0,0,31,31,0,0,0
	.byte 24,24,24,31,15,0,0,0
	.byte 0,0,0,24,24,24,24,24
	.byte 24,24,24,24,24,24,24,24
	.byte 0,0,0,15,31,24,24,24
	.byte 24,24,24,31,31,24,24,24
	.byte 0,0,0,248,248,0,0,0
	.byte 24,24,24,248,240,0,0,0
	.byte 0,0,0,255,255,0,0,0
	.byte 24,24,24,255,255,0,0,0
	.byte 0,0,0,240,248,24,24,24
	.byte 24,24,24,248,248,24,24,24
	.byte 0,0,0,255,255,24,24,24
	.byte 24,24,24,255,255,24,24,24
	.byte 16,56,108,198,0,0,0,0
	.byte 12,24,48,0,0,0,0,0
	.byte 102,102,0,0,0,0,0,0
	.byte 60,102,96,248,96,102,254,0
	.byte 56,68,186,162,186,68,56,0
	.byte 126,244,244,116,52,52,52,0
	.byte 30,48,56,108,56,24,240,0
	.byte 24,24,12,0,0,0,0,0
	.byte 64,192,68,76,84,30,4,0
	.byte 64,192,76,82,68,8,30,0
	.byte 224,16,98,22,234,15,2,0
	.byte 0,24,24,126,24,24,126,0
	.byte 24,24,0,126,0,24,24,0
	.byte 0,0,0,126,6,6,0,0
	.byte 24,0,24,48,102,102,60,0
	.byte 24,0,24,24,24,24,24,0
	.byte 0,0,115,222,204,222,115,0
	.byte 124,198,198,252,198,198,248,192
	.byte 0,102,102,60,102,102,60,0
	.byte 60,96,96,60,102,102,60,0
	.byte 0,0,30,48,124,48,30,0
	.byte 56,108,198,254,198,108,56,0
	.byte 0,192,96,48,56,108,198,0
	.byte 0,0,102,102,102,124,96,96
	.byte 0,0,0,254,108,108,108,0
	.byte 0,0,0,126,216,216,112,0
	.byte 3,6,12,60,102,60,96,192
	.byte 3,6,12,102,102,60,96,192
	.byte 0,230,60,24,56,108,199,0
	.byte 0,0,102,195,219,219,126,0
	.byte 254,198,96,48,96,198,254,0
	.byte 0,124,198,198,198,108,238,0
	.byte 24,48,96,192,128,0,0,0
	.byte 24,12,6,3,1,0,0,0
	.byte 0,0,0,1,3,6,12,24
	.byte 0,0,0,128,192,96,48,24
	.byte 24,60,102,195,129,0,0,0
	.byte 24,12,6,3,3,6,12,24
	.byte 0,0,0,129,195,102,60,24
	.byte 24,48,96,192,192,96,48,24
	.byte 24,48,96,193,131,6,12,24
	.byte 24,12,6,131,193,96,48,24
	.byte 24,60,102,195,195,102,60,24
	.byte 195,231,126,60,60,126,231,195
	.byte 3,7,14,28,56,112,224,192
	.byte 192,224,112,56,28,14,7,3
	.byte 204,204,51,51,204,204,51,51
	.byte 170,85,170,85,170,85,170,85
	.byte 255,255,0,0,0,0,0,0
	.byte 3,3,3,3,3,3,3,3
	.byte 0,0,0,0,0,0,255,255
	.byte 192,192,192,192,192,192,192,192
	.byte 255,254,252,248,240,224,192,128
	.byte 255,127,63,31,15,7,3,1
	.byte 1,3,7,15,31,63,127,255
	.byte 128,192,224,240,248,252,254,255
	.byte 170,85,170,85,0,0,0,0
	.byte 10,5,10,5,10,5,10,5
	.byte 0,0,0,0,170,85,170,85
	.byte 160,80,160,80,160,80,160,80
	.byte 170,84,168,80,160,64,128,0
	.byte 170,85,42,21,10,5,2,1
	.byte 1,2,5,10,21,42,85,170
	.byte 0,128,64,160,80,168,84,170
	.byte 126,255,153,255,189,195,255,126
	.byte 126,255,153,255,195,189,255,126
	.byte 56,56,254,254,254,16,56,0
	.byte 16,56,124,254,124,56,16,0
	.byte 108,254,254,254,124,56,16,0
	.byte 16,56,124,254,254,16,56,0
	.byte 0,60,102,195,195,102,60,0
	.byte 0,60,126,255,255,126,60,0
	.byte 0,126,102,102,102,102,126,0
	.byte 0,126,126,126,126,126,126,0
	.byte 15,7,13,120,204,204,204,120
	.byte 60,102,102,102,60,24,126,24
	.byte 12,12,12,12,12,60,124,56
	.byte 24,28,30,27,24,120,248,112
	.byte 153,90,36,195,195,36,90,153
	.byte 16,56,56,56,56,56,124,214
	.byte 24,60,126,255,24,24,24,24
	.byte 24,24,24,24,255,126,60,24
	.byte 16,48,112,255,255,112,48,16
	.byte 8,12,14,255,255,14,12,8
	.byte 0,0,24,60,126,255,255,0
	.byte 0,0,255,255,126,60,24,0
	.byte 128,224,248,254,248,224,128,0
	.byte 2,14,62,254,62,14,2,0
	.byte 56,56,146,124,16,40,40,40
	.byte 56,56,16,254,16,40,68,130
	.byte 56,56,18,124,144,40,36,34
	.byte 56,56,144,124,18,40,72,136
	.byte 0,60,24,60,60,60,24,0
	.byte 60,255,255,24,12,24,48,24
	.byte 24,60,126,24,24,126,60,24
	.byte 0,36,102,255,102,36,0,0

	.end
