Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Last active July 4, 2024 10:39
Show Gist options
  • Save neuro-sys/fb9181ff8064c75b9a5e89004eee207a to your computer and use it in GitHub Desktop.
Save neuro-sys/fb9181ff8064c75b9a5e89004eee207a to your computer and use it in GitHub Desktop.
;-----------------------------------------------------------------------------+
; Notes |
;-----------------------------------------------------------------------------+
; - Changing R12/R13 takes affect only when C4=C9=0, but should be set
; when C4>0 for compatibility reasons.
; - Changing R4 takes effect as long as C4 > R4' (new value).
; - Vsync starts when C4=C7
;-----------------------------------------------------------------------------+
;-----------------------------------------------------------------------------+
; Program start
;-----------------------------------------------------------------------------+
org 0x8000
jp start
;-----------------------------------------------------------------------------+
; Constants
;-----------------------------------------------------------------------------+
GA2 equ 0x8d ; ROMs disabled, Mode 1
;-----------------------------------------------------------------------------+
; Data Area
;-----------------------------------------------------------------------------+
;-----------------------------------------------------------------------------+
; Macro Definitions
;-----------------------------------------------------------------------------+
; (B-1) * 4[djnz when b!=0] + 3[djnz when b=0] => (b-1) * 4 + 3
; B = ((us - 3) / 4) + 1
; B = ((us - 3 - extra) / 4) + 1
; Spend extra time before or after the routine
; e.g. SLEEP 64
macro SLEEP us
let extra=(4 - ({us} - 3) % 4)
repeat extra
nop
rend
ld b, (({us} - 3 - 2 - extra ) / 4) + 1
djnz $
mend
macro CRTC_SET r, v
ld bc, 0xbc00+{r} ; 3
out (c), c ; 3
inc b ; 1
ld c, {v} ; 2
out (c), c ; 3 = 12
mend
macro BORDER_SET c
ld bc, 0x7f10
out (c), c
ld c, 0x40 | {c}
out (c), c
mend
macro GA_SET v
ld bc, 0x7f00 | {v} ; 3
out (c), c ; 3 = 6
mend
;-----------------------------------------------------------------------------+
; Routines
;-----------------------------------------------------------------------------+
wvsync ld b, 0xf5
wvsync2 in a, (c)
rra
jr nc, wvsync2
ret
stbint0
di
im 0
ld hl, 0xc9fb
ld (0x38), hl ; Disable interrupts
ei
ret
;-----------------------------------------------------------------------------+
; Program Start
;-----------------------------------------------------------------------------+
start
call stbint0
GA_SET GA2
BORDER_SET 20
CRTC_SET 6, 0xff
CRTC_SET 3, 0x88
CRTC_SET 4, 39-1
CRTC_SET 7, 0
;-----------------------------------------------------------------------------+
; Frame Begin
;-----------------------------------------------------------------------------+
frame
call wvsync
;-----------------------------------------------------------------------------+
; Vertical Sync start
;-----------------------------------------------------------------------------+
; It will last 8 lines (i.e. value in high byte of R3)
;-----------------------------------------------------------------------------+
GA_SET GA2 | 0x10 ; (6) Clear interrupt
CRTC_SET 7, 0xFF ; (12) Disable VSYNC
CRTC_SET 6, 0 ; (12) Disable DISPTMG
;-----------------------------------------------------------------------------+
; Display 9 Vertical Characters, i.e. 72 lines
;-----------------------------------------------------------------------------+
CRTC_SET 4, 9-1 ; (12) 9 VC, 72 VL
CRTC_SET 6, 25-1 ; (12) Keep DISPTMG on for 25 VC
halt ; 6.5 VC, 52 VL
;-----------------------------------------------------------------------------+
; C4=6, C9=5, i.e. Safe to change R12/R13 for next field
;-----------------------------------------------------------------------------+
CRTC_SET 12, 0x30
CRTC_SET 13, 0
repeat 20
SLEEP 64
rend ; 20 => 9 VC, 72 VL
;-----------------------------------------------------------------------------+
; Display 30 Vertical Characters, i.e. 240 lines
;-----------------------------------------------------------------------------+
CRTC_SET 4, 30-1
halt ; 13 VC, 104 VL
halt ; 19.5 VC, 156 VL
halt ; 26 VC, 208 VL
halt ; 32.5 VC, 260 VL
CRTC_SET 7, 0
jp frame
;-----------------------------------------------------------------------------+
; Heap Data Area
;-----------------------------------------------------------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment