Skip to content

Instantly share code, notes, and snippets.

@ped7g
Created October 29, 2019 16:18
Show Gist options
  • Save ped7g/877db9fe5b31c1787bc0bd955131ff49 to your computer and use it in GitHub Desktop.
Save ped7g/877db9fe5b31c1787bc0bd955131ff49 to your computer and use it in GitHub Desktop.
utility routines for ZX Spectrum Next (mostly to detect current video mode) (not tested properly yet)
; syntax for https://github.com/z00m128/sjasmplus/releases assembler
OPT --syntax=abfw --zxnext
;;----------------------------------------------------------------------------------------
;; reads nextreg in A into A
ReadNextReg:
; Input
; A = nextreg to read
; Output:
; A = value in nextreg
; Uses:
; A, [currently selected NextReg on I/O port $243B]
push bc
ld bc, $243B ; TBBLUE_REGISTER_SELECT_P_243B
out (c),a
inc b ; bc = TBBLUE_REGISTER_ACCESS_P_253B
in a,(c) ; read desired NextReg state
pop bc
ret
;;----------------------------------------------------------------------------------------
;; Detect current video mode:
;; 0, 1, 2, 3 = HDMI, ZX48, ZX128, Pentagon (all 50Hz), add +4 for 60Hz modes
;; (Pentagon 60Hz is not a valid mode => value 7 shouldn't be returned in A)
DetectMode:
; Output:
; A = current mode 0..6
; Uses:
; A, B, side effect: selects NextReg $11 or $03 on I/O port
;; read current configuration from NextRegs and convert it to 0..6 value
; read 50Hz/60Hz info
ld a,$05 ; PERIPHERAL_1_NR_05
call ReadNextReg
and $04 ; bit 2 = 50Hz/60Hz configuration
ld b,a ; remember the 50/60 as +0/+4 value in B
; read HDMI vs VGA info
ld a,$11 ; VIDEO_TIMING_NR_11
call ReadNextReg
inc a ; HDMI is value %111 in bits 2-0 -> zero it
and $07
jr z,.hdmiDetected
; if VGA mode, read particular zx48/zx128/pentagon setting
ld a,$03
call ReadNextReg
; a = bits 6-4: %00x zx48, %01x zx128, %100 pentagon
swapnib a
rra
inc a
and $03 ; A = 1/2/3 for zx48/zx128/pentagon
.hdmiDetected: add a,b ; add 50/60Hz value to final result
ret
;;----------------------------------------------------------------------------------------
;; Detect if the code is running inside CSpect emulator
DetectCSpectEmulator:
; Output:
; ZF=1 => CSpect, ZF=0 => HW board (also B=0 for CSpect, B=1 for HW)
; Uses:
; BC
ld b,-1
db $DD, $01, $00, $00
; CSpect will see: break, nop, nop
; Z80/Z80N HW will see: <wrong prefix> ld bc,0
inc b ; ZF=1 for CSpect, ZF=0 for HW board
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment