# HG changeset patch
# User John Tsiombikas <nuclear@member.fsf.org>
# Date 1489474963 -7200
# Node ID 2560a8be8cb8348534ab60ba70a3c4b9e46adbf6
# Parent  ce1b05082ac4a979f27f0240c58369dfbdaf758a
hblank interrupt test

diff -r ce1b05082ac4 -r 2560a8be8cb8 Makefile
--- a/Makefile	Tue Mar 14 05:59:33 2017 +0200
+++ b/Makefile	Tue Mar 14 09:02:43 2017 +0200
@@ -10,7 +10,7 @@
 warn = -pedantic -Wall
 dbg = -g
 def = -DGAMENAME=\"testgame\" -DVERSTR=\"01\" -D__NO_CTYPE
-inc = -Isrc/libc
+inc = -Isrc -Isrc/libc
 
 tool_prefix = m68k-linux-gnu-
 
@@ -21,7 +21,7 @@
 
 CFLAGS = -m68000 -ffreestanding -fno-builtin $(warn) $(dbg) $(opt) $(def) $(inc)
 CPPFLAGS = $(def)
-ASFLAGS = -m68000
+ASFLAGS = -m68000 $(inc)
 LDFLAGS = -T megadrive.ldscript -print-gc-sections \
 		  -L/usr/lib/gcc-cross/m68k-linux-gnu/6 -lgcc
 
diff -r ce1b05082ac4 -r 2560a8be8cb8 src/intr.s
--- a/src/intr.s	Tue Mar 14 05:59:33 2017 +0200
+++ b/src/intr.s	Tue Mar 14 09:02:43 2017 +0200
@@ -71,12 +71,36 @@
 | from here on we continue in the regular .text section since we don't care
 | where this code ends up.
 	.text
+
+.global enable_intr
+enable_intr:
+	andi.w #0xf8ff, %sr
+	rts
+
+.global disable_intr
+disable_intr:
+	ori.w #0x0300, %sr
+	rts
+
 | interrupt handlers
 intr_fatal:
 	stop #0x2700
 
-| TODO hblank/vblank code
+	.include "vdpdefs.inc"
+	.extern hblank_handler
+	.extern vblank_handler
+	.extern palval
+
 intr_hblank:
+	move.l #0xc0020000, VDP_PORT_CTL
+
+	move.w testcol, %d0
+	move.w %d0, VDP_PORT_DATA
+	rol.b #4, %d0
+	move.w %d0, testcol
+
 	rte
+
 intr_vblank:
+	jsr vblank_handler
 	rte
diff -r ce1b05082ac4 -r 2560a8be8cb8 src/main.c
--- a/src/main.c	Tue Mar 14 05:59:33 2017 +0200
+++ b/src/main.c	Tue Mar 14 09:02:43 2017 +0200
@@ -1,13 +1,31 @@
+#include <stdint.h>
 #include "vdp.h"
 
 int main(void)
 {
 	vdp_init();
 
+	vdp_set_pal_entry(0, 0, 0, 0, 0);
 	vdp_set_pal_entry(0, 1, 7, 0, 3);
 	vdp_set_bgcolor(0, 1);
 
+	vdp_enable_hintr(12);
+	vdp_enable_vintr();
+
 	for(;;);
 
 	return 0;
 }
+
+uint16_t testcol = 0x00c0;
+
+void hblank_handler(void)
+{
+	vdp_set_pal_entry(0, 1, ~testcol, 0, testcol);
+	testcol = ~testcol;
+}
+
+void vblank_handler(void)
+{
+	testcol = 0x00c0;
+}
diff -r ce1b05082ac4 -r 2560a8be8cb8 src/startup.s
--- a/src/startup.s	Tue Mar 14 05:59:33 2017 +0200
+++ b/src/startup.s	Tue Mar 14 09:02:43 2017 +0200
@@ -4,7 +4,7 @@
 	.global start
 	.global halt_cpu
 start:
-	bsr.s disable_intr
+	jsr disable_intr
 
 	| copy .data section from ROM to RAM
 	move.l #_data_lma, %a0
@@ -30,18 +30,8 @@
 	| setup the stack pointer stack
 	move.l #_stacktop, %sp
 	| now that we have a stack, we can enable interrupts
-	bsr.s enable_intr
+	jsr enable_intr
 
 	jsr main
 halt_cpu:
 	stop #0x2700
-
-.global enable_intr
-enable_intr:
-	andi.w #0xf8ff, %sr
-	rts
-
-.global disable_intr
-disable_intr:
-	ori.w #0x0300, %sr
-	rts
diff -r ce1b05082ac4 -r 2560a8be8cb8 src/vdp.h
--- a/src/vdp.h	Tue Mar 14 05:59:33 2017 +0200
+++ b/src/vdp.h	Tue Mar 14 09:02:43 2017 +0200
@@ -20,7 +20,7 @@
 	VDP_REG_NAMETAB_B		= 4,
 	VDP_REG_SPRITE_TAB		= 5,
 	VDP_REG_BGCOL			= 7,
-	VDP_REG_INTR			= 10,
+	VDP_REG_HINTR			= 10,
 	VDP_REG_MODE3			= 11,
 	VDP_REG_MODE4			= 12,
 	VDP_REG_SCROLL_TAB		= 13,
@@ -120,7 +120,7 @@
 
 static inline void vdp_setreg(int reg, uint8_t value)
 {
-	/*vdp_reg_shadow[reg] = value;*/
+	vdp_reg_shadow[reg] = value;
 	VDP_PORT_CTL = (uint16_t)value | (reg << 8) | (uint16_t)0x8000;
 }
 
@@ -152,6 +152,27 @@
 	VDP_PORT_DATA = VDP_PACK_RGB(r, g, b);
 }
 
+static inline void vdp_enable_vintr(void)
+{
+	vdp_setreg(VDP_REG_MODE2, vdp_getreg(VDP_REG_MODE2) | VDP_MODE2_VINTR);
+}
+
+static inline void vdp_disable_vintr(void)
+{
+	vdp_setreg(VDP_REG_MODE2, vdp_getreg(VDP_REG_MODE2) & ~VDP_MODE2_VINTR);
+}
+
+static inline void vdp_enable_hintr(int counter)
+{
+	vdp_setreg(VDP_REG_HINTR, counter);
+	vdp_setreg(VDP_REG_MODE1, vdp_getreg(VDP_REG_MODE1) | VDP_MODE1_HINTR);
+}
+
+static inline void vdp_disable_hintr(void)
+{
+	vdp_setreg(VDP_REG_MODE1, vdp_getreg(VDP_REG_MODE1) & ~VDP_MODE1_HINTR);
+}
+
 void vdp_init(void);
 
 #endif	/* VDP_H_ */
diff -r ce1b05082ac4 -r 2560a8be8cb8 src/vdpdefs.inc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/vdpdefs.inc	Tue Mar 14 09:02:43 2017 +0200
@@ -0,0 +1,13 @@
+| vi:filetype=asm68k:
+	.equ VDP_PORT_DATA, 0xc00000
+	.equ VDP_PORT_CTL, 0xc00004
+
+	.equ VDP_REG_MODE1, 0
+	.equ VDP_REG_MODE2, 1
+
+	.equ VDP_WRITE_VRAM, 0x40000000
+	.equ VDP_WRITE_CRAM, 0xc0000000
+	.equ VDP_WRITE_VSRAM, 0x40000010
+	.equ VDP_READ_VRAM, 0
+	.equ VDP_READ_CRAM, 0x00000020
+	.equ VDP_READ_VSRAM, 0x00000010