Post by jusalak on Apr 13, 2023 20:30:01 GMT
I just created an example of Z80 raster programming. Same principles apply as with 6510/8502, that is disable CIA interrupt, enable VIC interrupt, and prepare the interrupt handler. This example uses Z80 IM 2.
The program creates a red stripe in the green border.
main .org $1c01
.byte $0c,$08,$0a,$00,$9e,$37,$31,$38,$31,$00,$00,$00
lda $ff00 ;
pha ; store RAM config to stack
sei ; disable interrupts
lda $ffee
pha
lda $ffef
pha
lda $fff0
pha
lda #$3e ;
sta $ff00 ; select RAM config for Z80
ldx #$00
loop_:
lda #$31 ;prepare z80 IM 2 jump table
sta $3000,x
inx
bne loop_
sta $3100,x
lda #$c3 ;store JP instruction at $3131
sta $3131
lda #$1b ;address lo
sta $3132
lda #$38 ;address hi
sta $3133
lda #$7f ; disable CIA interrupt
sta $dc0d
lda $d01a ;enable raster interrupt
ora #$01
sta $d01a
lda $d011 ;clear raster counter high bit
and #$7f
sta $d011
lda #$c3 ;
sta $ffee ; store JP instruction for Z80 mode start
lda #<z80code ;
sta $ffef ; store lo-byte address
lda #>z80code;
sta $fff0 ; store hi-byte address
lda $d505 ;
pha ; store mode to stack
lda #$b0 ;
sta $d505 ; set Z80 mode - this instruction deactivates 8502 and jumps by Z80 PC to $ffee
nop
nop
pla
sta $d505
pla
sta $fff0
pla
sta $ffef
pla
sta $ffee
pla
sta $ff00
cli
rts
z80code
.byte $01 $00 $02; ld bc,0200h
.byte $11 $00 $38; ld de,3800h
.byte $21; ld hl
.byte <endcode
.byte >endcode
.byte $ED $B0; ldir
.byte $C3 $00 $38; jp 3800h
endcode
The other part
I can't attach the .prg file here but the hex dump is
.org 3800h
3800 31 F0 37 ld sp,37F0h ;set up stack pointer
3803 3E 30 ld a,30h ;set up I register
3805 ED 47 ld i,a
3807 ED 5E im 2 ;set interrupt mode 2
3809 01 20 D0 ld bc,0D020h ;set border color
380C 3E 05 ld a,05h
380E ED 79 out (c),a
3810 01 12 D0 ld bc,0D012h ;set up first interrupt line
3813 3E 64 ld a,64h
3815 ED 79 out (c),a
3817 FB ei ; enable interrupt
3818 00 loop_: nop
3819 18 FD jr loop_ ;do nothing loop
irq1: ;handler 1
381B F5 push af
381C C5 push bc
381D 01 3E 38 ld bc,irq2 ;load handler 2 address to the JP instruction
3820 ED 43 32 31 ld (3132h),bc
3824 01 20 D0 ld bc,0d020h
3827 3E 02 ld a,02h ;red border
3829 ED 79 out (c),a
382B 01 12 D0 ld bc,0d012h ;set lower raster line
382E 3E 84 ld a,84h
3830 ED 79 out (c),a
3832 01 19 D0 ld bc,0d019h ;acknowledge interrupt
3835 3E FF ld a,0ffh
3837 ED 79 out (c),a
3839 C1 pop bc
383A F1 pop af
383B FB ei
383C ED 4D reti
irq2: ;handler2
383E F5 push af
383F C5 push bc
3840 01 1B 38 ld bc,irq1 ;load handler 1 address to the JP instruction
3843 ED 43 32 31 ld (3132h),bc
3847 01 20 D0 ld bc,0d020h
384A 3E 05 ld a,05h ;green border
384C ED 79 out (c),a
384E 01 12 D0 ld bc,0d012h ;set again the upper raster line
3851 3E 64 ld a,64h
3853 ED 79 out (c),a
3855 01 19 D0 ld bc,0d019h ;acknowledge interrupt
3858 3E FF ld a,0ffh
385A ED 79 out (c),a
385C C1 pop bc
385D F1 pop af
385E FB ei
385F ED 4D reti
.end
I can't attach the .prg file here but the hex dump is
>01c00 00 0b 1c 0a 00 9e 37 31
>01c08 38 31 00 00 00 ad 00 ff
>01c10 48 78 ad ee ff 48 ad ef
>01c18 ff 48 ad f0 ff 48 a9 3e
>01c20 8d 00 ff a2 00 a9 31 9d
>01c28 00 30 e8 d0 f8 9d 00 31
>01c30 a9 c3 8d 31 31 a9 1b 8d
>01c38 32 31 a9 38 8d 33 31 a9
>01c40 7f 8d 0d dc ad 1a d0 09
>01c48 01 8d 1a d0 ad 11 d0 29
>01c50 7f 8d 11 d0 a9 c3 8d ee
>01c58 ff a9 84 8d ef ff a9 1c
>01c60 8d f0 ff ad 05 d5 48 a9
>01c68 b0 8d 05 d5 ea ea 68 8d
>01c70 05 d5 68 8d f0 ff 68 8d
>01c78 ef ff 68 8d ee ff 68 8d
>01c80 00 ff 58 60 01 00 02 11
>01c88 00 38 21 92 1c ed b0 c3
>01c90 00 38 31 f0 37 3e 30 ed
>01c98 47 ed 5e 01 20 d0 3e 05
>01ca0 ed 79 01 12 d0 3e 64 ed
>01ca8 79 fb 00 18 fd f5 c5 01
>01cb0 3e 38 ed 43 32 31 01 20
>01cb8 d0 3e 02 ed 79 01 12 d0
>01cc0 3e 84 ed 79 01 19 d0 3e
>01cc8 ff ed 79 c1 f1 fb ed 4d
>01cd0 f5 c5 01 1b 38 ed 43 32
>01cd8 31 01 20 d0 3e 05 ed 79
>01ce0 01 12 d0 3e 64 ed 79 01
>01ce8 19 d0 3e ff ed 79 c1 f1
>01cf0 fb ed 4d 00 00 00 00 00
This can be copy-pasted directly to x128 and Z64K; type first MONITOR. When the dump has been pasted, it can be saved by a MONITOR command
S "Z80RASTER",8,01C01,01CF3
Works on real hw, VICE and Z64K.