Jump to content

Light pen driver: Difference between revisions

From CPCWiki - THE Amstrad CPC encyclopedia!
Polluks (talk | contribs)
Created page with "<pre> ; z80dasm 1.2.0 ; command line: z80dasm -g0x9c40 -l MCPEN.BIN org 09c40h sub_9c40h: jr $+8 nop add a,003h ld de,(la250h)..."
 
Polluks (talk | contribs)
label incomplete
 
(18 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<pre>
<pre>
; z80dasm 1.2.0
;; Dissassembled and commented from MCPEN.BIN
; command line: z80dasm -g0x9c40 -l MCPEN.BIN


         org 09c40h
         org 09c40h


sub_9c40h:
sub_9c40h:
        jr $+8
         call 0bd19h ;; mc wait flyback
        nop
        add a,003h
        ld de,(la250h)
        ld (la248h+1),hl
        ld (05a94h),de
        ret
        or b
        ld (bc),a
        ld c,b
        ld (bc),a
        cp (hl)
        nop
        jr nz,l9c5ah
        inc b
l9c5ah:
        jr nz,l9c5dh
        cp h
l9c5dh:
        ld a,(de)
l9c5eh:
        inc c
        jr z,l9c6eh
        nop
        nop
        ld d,h
        or c
        add hl,hl
        ld bc,020a1h
        dec c
        nop
        nop
        ld d,h
        or c
l9c6eh:
        xor 019h
        add hl,de
        jr nz,l9c5eh
        jr nz,$+15
        nop
        nop
        ld d,h
        or c
        rst 28h
        add hl,de
        ld a,(de)
        nop
        ld b,000h
        or d
         call 0bd19h
         di
         di
         ld bc,0f40eh
         ld bc,0f40eh ;; we want to select AY register 14
         out (c),c
         out (c),c
         ld b,0f6h
         ld b,0f6h
Line 63: Line 13:
         and 030h
         and 030h
         ld c,a
         ld c,a
         or 0c0h
         or 0c0h   ;; AY control select register.
        out (c),a
sub_9c94h:
         out (c),a
         out (c),a
        out (c),a ;; BUG. Should be out (c),c to be compatible with Plus.
         inc b
         inc b
         ld a,092h
         ld a,092h  
         out (c),a
         out (c),a
         push bc
         push bc
         ld bc,0f649h
         ld bc,0f649h ;; AY control read from register and select keyboard line 9 (joystick)
        out (c),c
         out (c),c
         out (c),c
        out (c),c ;; BUG? No need to do this twice.
         ld b,0f4h
         ld b,0f4h
         ld hl,0fa66h
 
l9ca8h:
;; loop reading joystick until input seen or timeout
 
         ld hl,0fa66h ;; timeout is 0-FA66 because it counts up from this starting point.
                    ;; timeout is 59A loops.
l9c68:
         in a,(c)
         in a,(c)
         cp 0fdh
         cp 0fdh ;; 11111101. Bits are '1' if 'no press', but '0' if pressed. checking joy 0 down.
         jr z,l9cb9h
         jr z,l9c79
sub_9caeh:
         inc l
         inc l
         jr nz,l9ca8h
         jr nz,l9c68
         inc h
         inc h
         jr nz,l9ca8h
         jr nz,l9c68
     
        ;; timeout. not detected
         ld hl,0ffffh
         ld hl,0ffffh
         jr l9cbdh
         jr l9cbdh
l9cb9h:
l9c79:
        ;; detected.
         ld de,00420h
         ld de,00420h
         add hl,de
         add hl,de
l9cbdh:
l9cbd:
         ld (0a410h),hl
         ld (data_0a410h),hl ;; detected 'position' or 'ffff' for not detected
         pop bc
         pop bc
sub_9cc1h:
         ld a,082h
         ld a,082h
         out (c),a
         out (c),a
         dec b
         dec b
         out (c),c
         out (c),c ;; Set AY control inactive.
         ei
         ei
         ret
         ret
         call 09da6h
 
sub_9c8ah:
         call sub_9da6h ;; screen to buffer
 
         call sub_9ce4h
         call sub_9ce4h
         call 09db2h
 
         call sub_9db2h ;; buffer to screen
         ret
         ret
;; HL = HL/DE
sub_9c94:
         ld a,h
         ld a,h
         ld c,l
         ld c,l
         ld b,010h
         ld b,010h ;; 16 bits.
         ld hl,00000h
         ld hl,00000h
l9cdbh:
l9cdbh:
Line 114: Line 75:
         sbc hl,de
         sbc hl,de
         jr nc,l9ce5h
         jr nc,l9ce5h
sub_9ce4h:
         add hl,de
         add hl,de
l9ce5h:
l9ce5h:
Line 124: Line 84:
         ld l,c
         ld l,c
         ret
         ret
;; HL = HL * DE
sub_9caeh:
         ld a,l
         ld a,l
         ld c,h
         ld c,h
         ld b,010h
         ld b,010h ;; 16 bits
         ld hl,00000h
         ld hl,00000h
l9cf5h:
l9cf5h:
Line 139: Line 102:
         djnz l9cf5h
         djnz l9cf5h
         ret
         ret
         call sub_9c40h
 
sub_9cc1h:
         call sub_9c40h ;; detect
         ld a,0ffh
         ld a,0ffh
         cp h
         cp h
         ret z
         ret z
        ;; detected.
         ld de,00002h
         ld de,00002h
         call sub_9c94h
         call sub_9c94h ;; HL = HL/DE
         ld de,00064h
         ld de,00064h
         call sub_9caeh
         call sub_9caeh ;; HL = HL*DE
         ld de,00084h
         ld de,00084h
         call sub_9c94h
         call sub_9c94h ;; HL = HL/DE
         ex de,hl
         ex de,hl
         ld hl,0018fh
         ld hl,0018fh
         sbc hl,de
         sbc hl,de
         ld (0a410h),hl
         ld (data_0a410),hl
         ret
         ret
sub_9ce4h:
         ld a,000h
         ld a,000h
         ld bc,01a1ah
         ld bc,01a1ah
         call 0bc32h
         call 0bc32h ;; scr set ink
         call 0bc14h
         call 0bc14h ;; scr clear
         ld a,001h
         ld a,001h
         call 0bbdeh
         call 0bbdeh ;; gra set pen
         call sub_9cc1h
         call sub_9cc1h ;; get position
         ld a,0ffh
         ld a,0ffh
         cp h
         cp h
Line 167: Line 136:
         and a
         and a
         sbc hl,de
         sbc hl,de
         jr nc,$+3
          
        ;; BUG? This is jumping just after 0x021 of LD HL and then executing two nops before LD DE,0000h
        ;;jr nc,$+2
        jr nc,l9d06
         ld hl,00000h
         ld hl,00000h
l9d06:
         ld de,00000h
         ld de,00000h
         call 0bbc0h
         call 0bbc0h ;; gra move absolute
         ld a,001h
         ld a,001h
         ld bc,01a1ah
         ld bc,01a1ah
         call 0bc32h
         call 0bc32h ;; scr set ink
         ld a,000h
         ld a,000h
         ld bc,00000h
         ld bc,00000h
         call 0bc32h
         call 0bc32h ;; scr set ink
         ld b,00ah
         ld b,00ah
         call 0bd19h
l9d1e:
         djnz $-4
         call 0bd19h ;; mc wait flyback
         ld b,020h
       
         ;; BUG? this is jumping back to the 0x0a in    LD B,0ah
        ;; djnz $-4
        djnz l9d1e
 
 
         ld b,020h ;; number of lines
l9d65h:
l9d65h:
         push bc
         push bc
         call 09d35h
         call sub_9d35h ;; draw lines
         call sub_9c40h
         call sub_9c40h ;;detect
         pop bc
         pop bc
         ld a,0ffh
         ld a,0ffh
Line 191: Line 170:
         djnz l9d65h
         djnz l9d65h
         ret
         ret
         ld bc,(0a418h)
 
sub_9d35h:
         ld bc,(data_0a418)
l9d79h:
l9d79h:
         push bc
         push bc
         ld de,00000h
         ld de,00000h
         ld hl,00014h
         ld hl,00014h
         call 0bbf9h
         call 0bbf9h ;; gra line relative
         ld de,(0a416h)
         ld de,(data_0a416)
         ld hl,0ffech
         ld hl,0ffech
         call 0bbc3h
         call 0bbc3h ;; gra move relative
         pop bc
         pop bc
         djnz l9d79h
         djnz l9d79h
         ret
         ret
l9d91h:
l9d91h:
         ld de,0ffech
         ld de,0ffech ;; -20
         ld hl,00000h
         ld hl,00000h
         call 0bbc3h
         call 0bbc3h ;; gra move relative
l9d9ah:
l9d9ah:
         ld a,000h
         ld a,000h
         call 0bbdeh
         call 0bbdeh ;; gra set pen
         call 09d35h
         call sub_09d35h ;; lines
         ld a,001h
         ld a,001h
         call 0bbdeh
         call 0bbdeh ;; gra set pen
         ld de,0ffech
         ld de,0ffech ;; -20
         ld hl,00000h
         ld hl,00000h
         call 0bbc3h
         call 0bbc3h ;; gra move relative
         ld bc,(0a420h)
         ld bc,(data_0a420)
l9db4h:
l9db4h:
         push bc
         push bc
         call sub_9cc1h
         call sub_9cc1h ;; get position
         ld a,0ffh
         ld a,0ffh
         cp h
         cp h
         jr nz,l9ddah
         jr nz,sub_9d9ah
         ld de,00000h
         ld de,00000h
         ld hl,00014h
         ld hl,00014h
         call 0bbf9h
         call 0bbf9h ;; gra line relative
         ld de,(0a416h)
         ld de,(data_0a416)
         ld hl,0ffech
         ld hl,0ffech ;; -20
         call 0bbc3h
         call 0bbc3h ;; gra move relative
         pop bc
         pop bc
         djnz l9db4h
         djnz l9db4h
         ld hl,0ffffh
         ld hl,0ffffh
         ld (0a410h),hl
         ld (data_0a410),hl
         ret
         ret
l9ddah:
sub_9d9ah:
         pop bc
         pop bc
         ld (0a410h),hl
         ld (data_0a410),hl
         call 0bbc6h
         call 0bbc6h ;; gra ask cursor
         ld (0a412h),de
         ld (data_0a412),de
         ret
         ret
sub_9de6h:
 
;; screen to buffer
;; c000->5bfe length 3fd0
sub_9da6h:
         ld bc,03fd0h
         ld bc,03fd0h
         ld hl,0c000h
         ld hl,0c000h
Line 246: Line 230:
         ldir
         ldir
         ret
         ret
;; buffer to screen
;; 5bfe->c000 length 3fd0
sub_9db2h:
         ld bc,03fd0h
         ld bc,03fd0h
         ld hl,05bfeh
         ld hl,05bfeh
Line 251: Line 240:
         ldir
         ldir
         ret
         ret
sub_9dbeh:
         push hl
         push hl
         push de
         push de
         call 09e08h
         call sub_09e08h
         ld (0a414h),hl
         ld (data_0a414),hl
         ld de,05398h
         ld de,05398h
         ld b,004h
         ld b,004h
l9e0bh:
l9e0bh:
         push bc
         push bc
         ld hl,(0a414h)
         ld hl,(data_0a414)
         ld c,b
         ld c,b
         ld b,000h
         ld b,000h
Line 270: Line 261:
         ld (de),a
         ld (de),a
         inc de
         inc de
         call 0bc29h
         call 0bc29h ;; scr prev line
         djnz l9e18h
         djnz l9e18h
         pop bc
         pop bc
Line 277: Line 268:
         pop hl
         pop hl
         ret
         ret
sub_9de6h:
         push hl
         push hl
         push de
         push de
Line 283: Line 276:
l9e2dh:
l9e2dh:
         push bc
         push bc
         ld hl,(0a414h)
         ld hl,(data_0a414)
         ld c,b
         ld c,b
         ld b,000h
         ld b,000h
Line 294: Line 287:
         ld (hl),a
         ld (hl),a
         inc de
         inc de
         call 0bc29h
         call 0bc29h ;; scr prev line
         djnz l9e3ah
         djnz l9e3ah
         pop bc
         pop bc
Line 301: Line 294:
         pop hl
         pop hl
         ret
         ret
sub_9e08h:
         push de
         push de
         ld de,00002h
         ld de,00002h
         call sub_9c94h
         call sub_9c94h ;; HL = HL/DE
         pop de
         pop de
         push hl
         push hl
         ex de,hl
         ex de,hl
l9e52h:
         ld de,(data_0a416)
         ld de,(0a416h)
         call sub_9c94h ;; HL = HL/DE
         call sub_9c94h
         ex de,hl
         ex de,hl
         pop hl
         pop hl
         call 0bc1dh
         call 0bc1dh ;; scr dot position
         ret
         ret
         call 09c8ah
 
         ld hl,(0a418h)
sub_9e1fh:
         ld (0a420h),hl
         call sub_9c8ah
         ld hl,(0a410h)
         ld hl,(data_0a418)
         ld (data_0a420h),hl
         ld hl,(data_0a410)
         ld a,0ffh
         ld a,0ffh
         cp h
         cp h
         ret z
         ret z
         ld (0a41ch),hl
         ld (data_a41c),hl
         ld de,(0a412h)
         ld de,(data_0a412)
         ld (0a41ah),de
         ld (data_a41a),de
l9e7ah:
l9e7ah:
         ld de,0000ah
         ld de,0000ah
Line 332: Line 328:
l9e85h:
l9e85h:
         push hl
         push hl
         ld hl,(0a412h)
         ld hl,(data_a412)
         add hl,de
         add hl,de
         ex de,hl
         ex de,hl
         pop hl
         pop hl
         call 09dbeh
         call sub_09dbeh
         call 0bbc0h
         call 0bbc0h ;; gra move absolute
         call 09ed2h
sub_9e52h:
         call sub_9de6h
         call sub_09ed2h
sub_9e98h:
         call sub_09de6h
         ld hl,(0a410h)
         ld hl,(data_a410)
         ld a,0ffh
         ld a,0ffh
         cp h
         cp h
         jr nz,l9ea9h
         jr nz,l9ea9h
         ld a,02fh
         ld a,02fh
         call 0bb1eh
         call 0bb1eh ;; km test key
         ret z
         ret z
         jp l9e52h
         jp sub_9e52h
 
l9ea9h:
l9ea9h:
         call 09eb7h
         call sub_09eb7h
         ld a,02fh
         ld a,02fh
         call 0bb1eh
         call 0bb1eh ;; km test key
         ret z
         ret z
         ld hl,(0a41ch)
         ld hl,(data_a41c)
         ld de,(0a41ah)
         ld de,(data_a41a)
         call 0bbc0h
         call 0bbc0h ;; gra move absolute
         ld hl,(0a410h)
         ld hl,(data_a410)
         ld de,(0a412h)
         ld de,(data_a412)
         ld a,(0a41eh)
         ld a,(data_a41e)
         call 0bbdeh
         call 0bbdeh ;; gra set pen
         ld (0a41ch),hl
         ld (data_a41c),hl
         ld (0a41ah),de
         ld (data_a41a),de
         call 0bbf6h
         call 0bbf6h ;; gra line absolute
         ld hl,(0a410h)
         ld hl,(0a410h)
         jr l9e7ah
         jr l9e7ah
sub_09e98h:
         ld de,0ffech
         ld de,0ffech
         ld hl,00000h
         ld hl,00000h
         call 0bbc3h
         call 0bbc3h ;; gra move relative
         ld a,001h
         ld a,001h
         call 0bbdeh
         call 0bbdeh ;; gra set pen
         call 09d35h
         call sub_09d35h
         ld a,000h
         ld a,000h
         call 0bbdeh
         call 0bbdeh ;; gra set pen
         call sub_9cc1h
         call sub_9cc1h ;; get position
         ld a,0ffh
         ld a,0ffh
         cp h
         cp h
         ret nz
         ret nz
l9ef5h:
         pop de
         pop de
         ret
         ret
         ld hl,(0a41ah)
 
         ld de,(0a412h)
sub_9eb7h:
l9efeh:
         ld hl,(data_a41a)
         ld de,(data_a412)
         and a
         and a
         sbc hl,de
         sbc hl,de
         ret z
         ret z
         jp p,09ecbh
         jp p,l9ecbh
 
         ld hl,l9f15h
         ld hl,l9f15h
         jp 09eceh
         jp l09eceh
         ld hl,09ed2h
 
l9f0eh:
l9ecbh:
         ld hl,sub_9ed2h
l9eceh:
         ld (l9e52h+1),hl
         ld (l9e52h+1),hl
         ret
         ret
sub_9ed2h:
         call sub_9e98h
         call sub_9e98h
l9f15h:
 
         ld de,0ffech
         ld de,0ffech
         ld hl,00000h
         ld hl,00000h
         call 0bbc3h
         call 0bbc3h ;; gra move relative
         ld bc,(0a420h)
         ld bc,(data_a420)
l9f22h:
         push bc
         push bc
         ld de,00000h
         ld de,00000h
         ld hl,00014h
         ld hl,00014h
         call 0bbf9h
         call 0bbf9h ;; gra line relative
         call sub_9cc1h
         call sub_9cc1h
         ld a,0ffh
         ld a,0ffh
         cp h
         cp h
         jp nz,l9efeh
         jp nz,sub_9efeh
         ld hl,(0a422h)
sub_9ef5h:
         ld (0a410h),hl
         ld hl,(data_a422)
         jp l9d9ah
         ld (data_a410),hl
         ld (0a422h),hl
         jp sub_9d9ah
         ld de,(0a416h)
 
sub_9efeh:
         ld (data_a422),hl
         ld de,(data_a416)
         ld hl,0ffech
         ld hl,0ffech
         call 0bbc3h
         call 0bbc3h ;; gra move relative
         pop bc
         pop bc
         djnz l9f22h
         djnz l9f22h
         ld hl,0ffffh
         ld hl,0ffffh
         ld (0a410h),hl
         ld (data_a410),hl
         ret
         ret
sub_9f82h:
         call sub_9e98h
         call sub_9e98h
         ld bc,(0a420h)
         ld bc,(data_a420)
l9f5ch:
l9f5ch:
         push bc
         push bc
         ld de,00000h
         ld de,00000h
         ld hl,00014h
         ld hl,00014h
         call 0bbf9h
         call 0bbf9h ;; gra line relative
         call sub_9cc1h
         call sub_9cc1h
         ld a,0ffh
         ld a,0ffh
         cp h
         cp h
         jp z,l9ef5h
         jp z,sub_9ef5h
         ld (0a422h),hl
 
         ld de,(0a416h)
         ld (data_0a422),hl
         ld de,(data_a416)
         ld hl,00000h
         ld hl,00000h
         and a
         and a
Line 440: Line 449:
         pop de
         pop de
         ld hl,0ffech
         ld hl,0ffech
         call 0bbc3h
         call 0bbc3h ;; gra move relative
         pop bc
         pop bc
         djnz l9f5ch
         djnz l9f5ch
         ld hl,00000h
         ld hl,00000h
         ld de,00014h
         ld de,00014h
         call 0bbc3h
         call 0bbc3h ;; gra move relative
         jp l9f0eh
         jp l9f0eh
        nop
 
         nop
         db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
         nop
         db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
         nop
         db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
         nop
         db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
         nop
         db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
         nop
         db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
         nop
         db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        nop
 
        nop
;; screen dump to printer.
        nop
sub_0a028h:
        nop
         ld a,013h ;; INC instruction
        nop
         ld (la124),a
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
la050h:
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
         ld a,013h
         ld (la124h),a
         ld a,00eh
         ld a,00eh
         ld (0a0e2h),a
         ld (la0e1+1),a
         ld a,(la133h)
         ld a,(data_a133)
         bit 1,a
         bit 1,a
         jr z,la083h
         jr z,la083h
         ld a,000h
         ld a,000h ;; NOP instruction
         ld (la124h),a
         ld (la124),a
         ld a,007h
         ld a,007h
         ld (0a0e2h),a
         ld (la0e1+1),a
la083h:
la083h:
         call 0bd28h
         call 0bd28h ;; mc reset printer
 
         ld de,00000h
         ld de,00000h
         ld hl,00000h
         ld hl,00000h
         ld (0a12dh),de
         ld (data_a12d),de
sub_a050h:
         ld a,000h
         ld a,000h
         ld (0a12ch),a
         ld (data_a12c),a ;; accumulated pixels.
         ld de,(0a12dh)
 
         call sub_a10dh
         ld de,(data_a12d)
         call 0bbf0h
         call sub_a10dh ;; store current graphics position
 
         call 0bbf0h ;; gra test absolute
         ld de,00001h
         ld de,00001h
         call sub_a11dh
         call sub_a11dh ;; get pixel
         ld de,00002h
         ld de,00002h
         call sub_a11dh
         call sub_a11dh ;; get pixel
         ld de,00004h
         ld de,00004h
         call sub_a11dh
         call sub_a11dh ;; get pixel
         ld de,00008h
         ld de,00008h
         call sub_a11dh
         call sub_a11dh ;; get pixel
         ld de,00010h
         ld de,00010h
         call sub_a11dh
         call sub_a11dh ;; get pixel
         ld de,00020h
         ld de,00020h
         call sub_a11dh
         call sub_a11dh ;; get pixel
         ld de,00040h
         ld de,00040h
         call sub_a0ffh
         call sub_a0ffh ;; get pixel
la0c9h:
la0c9h:
         call 0bd2eh
         call 0bd2eh ;; mc busy printer
         jr c,la0c9h
         jr c,la0c9h
         call 0bd28h
         call 0bd28h ;; mc reset printer
         ld a,01bh
         ld a,01bh   ;; ESC
         call 0bd2bh
         call 0bd2bh ;; mc print char
         ld a,04bh
         ld a,04bh   ;; K
         call 0bd2bh
         call 0bd2bh ;; mc print char
         ld a,000h
         ld a,000h   ;; number of columns low byte
         call 0bd2bh
         call 0bd2bh ;; mc print char
         ld a,(la133h)
         ld a,(data_a133)
         bit 0,a
         bit 0,a
         jr z,la0f4h
         jr z,la0b4h
         ld a,002h
         ld a,002h   ;; number of columns high byte?
         call 0bd2bh
         call 0bd2bh ;; mc print char
         ld a,(0a12ch)
 
         call 0bd2bh
         ld a,(data_a12c) ;; pixel data
         call 0bd2bh ;; mc print char
         jr la0f9h
         jr la0f9h
la0f4h:
la0b4h:
         ld a,001h
         ld a,001h   ;; number of columns high byte?
         call 0bd2bh
         call 0bd2bh ;; mc print char
la0f9h:
la0f9h:
         ld a,(0a12ch)
         ld a,(data_a12c) ;; pixel data
         call 0bd2bh
         call 0bd2bh ;; mc print char
sub_a0ffh:
 
         call sub_a115h
         call sub_a115h
         inc hl
         inc hl
Line 727: Line 538:
         ld a,h
         ld a,h
         cp 001h
         cp 001h
         jp nz,la050h
         jp nz,sub_a050h
sub_a10dh:
         ld a,l
         ld a,l
         cp 090h
         cp 090h
         jp nz,la050h
         jp nz,sub_a050h
         ld a,00ah
         ld a,00ah   ;; next line
sub_a115h:
         call 0bd2bh ;; mc print char
         call 0bd2bh
 
         ld a,02fh
         ld a,02fh ;; 47 - space
         call 0bb1eh
         call 0bb1eh ;; km test key
sub_a11dh:
         ret nz
         ret nz
         ld hl,(0a12dh)
         ld hl,(data_a12d)
la0e1:
         ld de,0000eh
         ld de,0000eh
la124h:
         add hl,de
         add hl,de
         ld (0a12dh),hl
         ld (data_a12d),hl
         ld hl,00000h
         ld hl,00000h
         call sub_a10dh
         call sub_a10dh
         ld de,(0a12dh)
         ld de,(data_a12d)
         ld a,d
         ld a,d
la133h:
         cp 002h
         cp 002h
         jp nz,la050h
         jp nz,sub_a050h
         ld a,e
         ld a,e
         cp 084h
         cp 084h
         jp nz,la050h
         jp nz,sub_a050h
         ret
         ret
;; accumulate pixels
sub_a0ffh:
         ld b,a
         ld b,a
         call 0bb99h
 
         cp b
         call 0bb99h ;; txt get paper
         cp b       ;; was the colour read the same as the paper?
         ret z
         ret z
         ld hl,(0a12ch)
 
        ;; any pixel which is not the same as the paper will be considered foreground
        ;; and the printer will draw a dot. Any pixel the same as the paper will
        ;; be considered background and no dot is drawn by the printer.
 
        ;; pixel is set to foreground. accumulate it into the pixel data so far
         ld hl,(data_a12c)
         add hl,de
         add hl,de
         ld (0a12ch),hl
         ld (data_a12c),hl
         ret
         ret
         ld (0a12fh),hl
 
         ld (0a131h),de
sub_a10dh:
         ld (data_a12f),hl
         ld (data_a131),de
         ret
         ret
         ld hl,(0a12fh)
 
         ld de,(0a131h)
sub_a115h:
         ld hl,(data_a12f)
         ld de,(data_a131)
         ret
         ret
         call sub_a0ffh
 
         call sub_a115h
sub_a11dh:
         inc de
         call sub_a0ffh ;; accumulate a pixel for printing.
         call sub_a115h ;; get current X/Y graphics position
         inc de         ;; increment X to next pixel accross screen.
la124:
         inc de
         inc de
         call sub_a10dh
         call sub_a10dh ;; set current X/Y graphics position
         call 0bbf0h
         call 0bbf0h ;; gra test absolute
         ret
         ret
         nop
data_a12c:
        add a,h
         db 00h
         ld (bc),a
data_a12d:
        nop
         db 84h,02h
         nop
data_a12f:
        ld c,000h
         db 00h,00h
        nop
data_a131:
         nop
         dw 000eh
        nop
data_a133:
         nop
         db 00h
        nop
data_a134:
         nop
         db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        nop
 
        nop
sub_a154h:
        nop
         call 0bc11h ;; scr get mode
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
la18bh:
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
         call 0bc11h
         cp 000h
         cp 000h
         jr nz,la19dh
         jr nz,la19dh
Line 831: Line 630:
         ld a,b
         ld a,b
la1aah:
la1aah:
         ld (0a254h),a
         ld (data_a254),a
         call 0bbc6h
         call 0bbc6h ;; gra ask cursor
         ld (la250h),hl
         ld (data_a250),hl
         ld (la250h+2),de
         ld (data_a250+2),de
la1b7h:
la1b7h:
         call 0bb99h
         call 0bb99h ;; txt get paper
         ld (la1f9h),a
         ld (pen_cmp+1),a
         ld bc,00000h
         ld bc,00000h
         ld (0a24ch),bc
         ld (data_a24c),bc
         ld (0a24eh),bc
         ld (data_a24e),bc
         call 0a210h
         call sub_a210
         ld a,02fh
la18bh:
         call 0bb1eh
         ld a,02fh ;; 47 - space
         call 0bb1eh ;; km test key
         ret nz
         ret nz
         ld hl,(0a24eh)
         ld hl,(data_a24e)
         inc hl
         inc hl
         inc hl
         inc hl
Line 853: Line 653:
         ld hl,00000h
         ld hl,00000h
la1deh:
la1deh:
         ld (0a24eh),hl
         ld (data_a24e),hl
         ld bc,05398h
         ld bc,05398h
         add hl,bc
         add hl,bc
         ld (la1aah),hl
         ld (buffer_addr3+2),hl ;; +2 to skip ED,53
buffer_addr3:
         ld de,(055e0h)
         ld de,(055e0h)
         ld hl,(0a24eh)
         ld hl,(data_a24e)
sub_a1efh:
sub_a1efh:
         ld bc,057e4h
         ld bc,057e4h
         add hl,bc
         add hl,bc
         ld (la1b7h),hl
         ld (buffer_addr4+1),hl ;; +1 to skip 22
buffer_addr4:
         ld hl,(05a2ch)
         ld hl,(05a2ch)
la1f9h:
         ld (data_a257),hl
         ld (la257h),hl
         ld (data_a255),de
         ld (0a255h),de
         ld bc,00000h
         ld bc,00000h
         ld a,(0a254h)
         ld a,(data_a254)
         ld c,a
         ld c,a
         ex de,hl
         ex de,hl
Line 885: Line 686:
         dec hl
         dec hl
         call sub_a1efh
         call sub_a1efh
         ld hl,(0a24eh)
         ld hl,(data_a24e)
         ld de,(0a24ch)
         ld de,(data_a24c)
         inc hl
         inc hl
         inc hl
         inc hl
Line 893: Line 694:
         ret z
         ret z
         jp la18bh
         jp la18bh
sub_a1efh:
         push de
         push de
         push hl
         push hl
         push bc
         push bc
         call 0bbf0h
         call 0bbf0h ;; gra test absolute
         pop bc
         pop bc
         pop hl
         pop hl
la237h:
         pop de
         pop de
pen_cmp:
         cp 000h
         cp 000h
         jr nz,la248h
         jr nz,la248h
         ld (la250h),hl
         ld (data_a250),hl
         ld (la250h+2),de
         ld (data_a252),de
         push bc
         push bc
         call 0a210h
         call sub_0a210h
         pop bc
         pop bc
la248h:
la248h:
         ld hl,(la257h)
         ld hl,(data_a257)
         ld de,(0a255h)
         ld de,(data_a255)
         ret
         ret
la250h:
sub_a210:
         ld hl,(la250h)
         ld hl,(data_a250)
         ld de,(la250h+2)
         ld de,(data_a252)
la257h:
         call 0bbeah ;; gra plot absolute
         call 0bbeah
         ld hl,(data_a24c)
         ld hl,(0a24ch)
         inc hl
         inc hl
         inc hl
         inc hl
Line 925: Line 727:
         ld hl,00000h
         ld hl,00000h
la267h:
la267h:
         ld (0a24ch),hl
         ld (data_a24c),hl
         ld bc,05398h
         ld bc,05398h
         add hl,bc
         add hl,bc
         ld de,(la250h+2)
         ld de,(data_a252)
         ld (la237h),hl
         ld (buffer_addr1+2),hl ;; +2 to skip ED, 53
buffer_addr1:
         ld (05648h),de
         ld (05648h),de
         ld hl,(0a24ch)
         ld hl,(data_a24c)
         ld bc,057e4h
         ld bc,057e4h
         add hl,bc
         add hl,bc
         ld de,(la250h)
         ld de,(data_a250)
         ld (la248h+1),hl
         ld (buffer_addr+2),hl ;; +2 to skip ED, 53
buffer_addr2:
         ld (05a94h),de
         ld (05a94h),de
         ret
         ret
        or b
        ld (bc),a
        ld c,b
        ld (bc),a
        cp (hl)
        nop
        jr nz,la295h
        inc b
la295h:
        jr nz,$+3
        cp h


         end</pre>
;;Data area
data_a24c:
        defw 02b0h
data_a24e:
        defw 0248h
data_a250:
        defw 0beh
data_a252:
        defw 0120h
data_a254:
        defb 04h
data_a255:
        defw 0120h
data_a257:
        defw 0bch
 
 
;; detected light pen position
data_a410:
        defw 0
data_a412:
        defw 0
;; memory/buffer address
data_a414:
        defw 0
data_a416:
        defw 0
data_a418:
        defw 0
 
;; gra x,y position
data_a41a:
        defw 0
data_a41c:
        defw 0
;; graphics pen
data_a41e:
        defb 0
        defb 0
data_a420:
        defw 0
         end
</pre>


[[Category:Source code]]
[[Category:Source code]]

Latest revision as of 07:30, 21 August 2025

;; Dissassembled and commented from MCPEN.BIN

        org 09c40h

sub_9c40h:
        call 0bd19h ;; mc wait flyback
        di
        ld bc,0f40eh ;; we want to select AY register 14
        out (c),c
        ld b,0f6h
        in a,(c)
        and 030h
        ld c,a
        or 0c0h   ;; AY control select register.
        out (c),a
        out (c),a ;; BUG. Should be out (c),c to be compatible with Plus.
        inc b
        ld a,092h 
        out (c),a
        push bc
        ld bc,0f649h ;; AY control read from register and select keyboard line 9 (joystick)
        out (c),c
        out (c),c ;; BUG? No need to do this twice.
        ld b,0f4h

;; loop reading joystick until input seen or timeout

        ld hl,0fa66h ;; timeout is 0-FA66 because it counts up from this starting point.
                     ;; timeout is 59A loops.
l9c68:
        in a,(c)
        cp 0fdh  ;; 11111101. Bits are '1' if 'no press', but '0' if pressed. checking joy 0 down.
        jr z,l9c79
        inc l
        jr nz,l9c68
        inc h
        jr nz,l9c68
      
        ;; timeout. not detected
        ld hl,0ffffh
        jr l9cbdh
l9c79:
        ;; detected.
        ld de,00420h
        add hl,de
l9cbd:
        ld (data_0a410h),hl ;; detected 'position' or 'ffff' for not detected
        pop bc
        ld a,082h
        out (c),a
        dec b
        out (c),c ;; Set AY control inactive.
        ei
        ret

sub_9c8ah:
        call sub_9da6h ;; screen to buffer

        call sub_9ce4h

        call sub_9db2h ;; buffer to screen
        ret

;; HL = HL/DE
sub_9c94:
        ld a,h
        ld c,l
        ld b,010h ;; 16 bits.
        ld hl,00000h
l9cdbh:
        rl c
        rla
        adc hl,hl
        sbc hl,de
        jr nc,l9ce5h
        add hl,de
l9ce5h:
        ccf
        djnz l9cdbh
        rl c
        rla
        ld h,a
        ld l,c
        ret

;; HL = HL * DE
sub_9caeh:
        ld a,l
        ld c,h
        ld b,010h ;; 16 bits
        ld hl,00000h
l9cf5h:
        srl c
        rra
        jr nc,l9cfbh
        add hl,de
l9cfbh:
        ex de,hl
        add hl,hl
        ex de,hl
        djnz l9cf5h
        ret

sub_9cc1h:
        call sub_9c40h ;; detect
        ld a,0ffh
        cp h
        ret z

        ;; detected.
        ld de,00002h
        call sub_9c94h ;; HL = HL/DE
        ld de,00064h
        call sub_9caeh ;; HL = HL*DE
        ld de,00084h
        call sub_9c94h ;; HL = HL/DE
        ex de,hl
        ld hl,0018fh
        sbc hl,de
        ld (data_0a410),hl
        ret

sub_9ce4h:
        ld a,000h
        ld bc,01a1ah
        call 0bc32h ;; scr set ink
        call 0bc14h ;; scr clear
        ld a,001h
        call 0bbdeh ;; gra set pen
        call sub_9cc1h ;; get position
        ld a,0ffh
        cp h
        ret z
        ld de,0000ah
        and a
        sbc hl,de
        
        ;; BUG? This is jumping just after 0x021 of LD HL and then executing two nops before LD DE,0000h
        ;;jr nc,$+2
        jr nc,l9d06
        ld hl,00000h
l9d06:
        ld de,00000h
        call 0bbc0h ;; gra move absolute
        ld a,001h
        ld bc,01a1ah
        call 0bc32h ;; scr set ink
        ld a,000h
        ld bc,00000h
        call 0bc32h ;; scr set ink
        ld b,00ah
l9d1e:
        call 0bd19h ;; mc wait flyback
        
        ;; BUG? this is jumping back to the 0x0a in     LD B,0ah
        ;; djnz $-4
        djnz l9d1e


        ld b,020h ;; number of lines
l9d65h:
        push bc
        call sub_9d35h ;; draw lines
        call sub_9c40h ;;detect
        pop bc
        ld a,0ffh
        cp h
        jr nz,l9d91h
        djnz l9d65h
        ret

sub_9d35h:
        ld bc,(data_0a418)
l9d79h:
        push bc
        ld de,00000h
        ld hl,00014h
        call 0bbf9h ;; gra line relative
        ld de,(data_0a416)
        ld hl,0ffech
        call 0bbc3h ;; gra move relative
        pop bc
        djnz l9d79h
        ret
l9d91h:
        ld de,0ffech ;; -20
        ld hl,00000h
        call 0bbc3h  ;; gra move relative
l9d9ah:
        ld a,000h
        call 0bbdeh ;; gra set pen
        call sub_09d35h ;; lines
        ld a,001h
        call 0bbdeh ;; gra set pen
        ld de,0ffech ;; -20
        ld hl,00000h
        call 0bbc3h ;; gra move relative
        ld bc,(data_0a420)
l9db4h:
        push bc
        call sub_9cc1h ;; get position
        ld a,0ffh
        cp h
        jr nz,sub_9d9ah
        ld de,00000h
        ld hl,00014h
        call 0bbf9h ;; gra line relative
        ld de,(data_0a416)
        ld hl,0ffech ;; -20
        call 0bbc3h ;; gra move relative
        pop bc
        djnz l9db4h
        ld hl,0ffffh
        ld (data_0a410),hl
        ret
sub_9d9ah:
        pop bc
        ld (data_0a410),hl
        call 0bbc6h ;; gra ask cursor
        ld (data_0a412),de
        ret

;; screen to buffer
;; c000->5bfe length 3fd0
sub_9da6h:
        ld bc,03fd0h
        ld hl,0c000h
        ld de,05bfeh
        ldir
        ret

;; buffer to screen
;; 5bfe->c000 length 3fd0

sub_9db2h:
        ld bc,03fd0h
        ld hl,05bfeh
        ld de,0c000h
        ldir
        ret

sub_9dbeh:
        push hl
        push de
        call sub_09e08h
        ld (data_0a414),hl
        ld de,05398h
        ld b,004h
l9e0bh:
        push bc
        ld hl,(data_0a414)
        ld c,b
        ld b,000h
        inc hl
        and a
        sbc hl,bc
        ld b,00bh
l9e18h:
        ld a,(hl)
        ld (de),a
        inc de
        call 0bc29h ;; scr prev line
        djnz l9e18h
        pop bc
        djnz l9e0bh
        pop de
        pop hl
        ret

sub_9de6h:
        push hl
        push de
        ld de,05398h
        ld b,004h
l9e2dh:
        push bc
        ld hl,(data_0a414)
        ld c,b
        ld b,000h
        inc hl
        and a
        sbc hl,bc
        ld b,00bh
l9e3ah:
        ld a,(de)
        ld (hl),a
        inc de
        call 0bc29h ;; scr prev line
        djnz l9e3ah
        pop bc
        djnz l9e2dh
        pop de
        pop hl
        ret

sub_9e08h:
        push de
        ld de,00002h
        call sub_9c94h ;; HL = HL/DE
        pop de
        push hl
        ex de,hl
        ld de,(data_0a416)
        call sub_9c94h ;; HL = HL/DE
        ex de,hl
        pop hl
        call 0bc1dh ;; scr dot position
        ret

sub_9e1fh:
        call sub_9c8ah
        ld hl,(data_0a418)
        ld (data_0a420h),hl
        ld hl,(data_0a410)
        ld a,0ffh
        cp h
        ret z
        ld (data_a41c),hl
        ld de,(data_0a412)
        ld (data_a41a),de
l9e7ah:
        ld de,0000ah
        and a
        sbc hl,de
        jr nc,l9e85h
        ld hl,00000h
l9e85h:
        push hl
        ld hl,(data_a412)
        add hl,de
        ex de,hl
        pop hl
        call sub_09dbeh
        call 0bbc0h ;; gra move absolute
sub_9e52h:
        call sub_09ed2h
        call sub_09de6h
        ld hl,(data_a410)
        ld a,0ffh
        cp h
        jr nz,l9ea9h
        ld a,02fh
        call 0bb1eh ;; km test key
        ret z
        jp sub_9e52h

l9ea9h:
        call sub_09eb7h
        ld a,02fh
        call 0bb1eh ;; km test key
        ret z
        ld hl,(data_a41c)
        ld de,(data_a41a)
        call 0bbc0h ;; gra move absolute
        ld hl,(data_a410)
        ld de,(data_a412)
        ld a,(data_a41e)
        call 0bbdeh ;; gra set pen
        ld (data_a41c),hl
        ld (data_a41a),de
        call 0bbf6h ;; gra line absolute
        ld hl,(0a410h)
        jr l9e7ah

sub_09e98h:
        ld de,0ffech
        ld hl,00000h
        call 0bbc3h ;; gra move relative
        ld a,001h
        call 0bbdeh ;; gra set pen
        call sub_09d35h
        ld a,000h
        call 0bbdeh ;; gra set pen
        call sub_9cc1h ;; get position
        ld a,0ffh
        cp h
        ret nz
        pop de
        ret

sub_9eb7h:
        ld hl,(data_a41a)
        ld de,(data_a412)
        and a
        sbc hl,de
        ret z
        jp p,l9ecbh

        ld hl,l9f15h
        jp l09eceh

l9ecbh:
        ld hl,sub_9ed2h
l9eceh:
        ld (l9e52h+1),hl
        ret

sub_9ed2h:
        call sub_9e98h

        ld de,0ffech
        ld hl,00000h
        call 0bbc3h ;; gra move relative
        ld bc,(data_a420)
        push bc
        ld de,00000h
        ld hl,00014h
        call 0bbf9h ;; gra line relative
        call sub_9cc1h
        ld a,0ffh
        cp h
        jp nz,sub_9efeh
sub_9ef5h:
        ld hl,(data_a422)
        ld (data_a410),hl
        jp sub_9d9ah

sub_9efeh:
        ld (data_a422),hl
        ld de,(data_a416)
        ld hl,0ffech
        call 0bbc3h ;; gra move relative
        pop bc
        djnz l9f22h
        ld hl,0ffffh
        ld (data_a410),hl
        ret

sub_9f82h:
        call sub_9e98h
        ld bc,(data_a420)
l9f5ch:
        push bc
        ld de,00000h
        ld hl,00014h
        call 0bbf9h ;; gra line relative
        call sub_9cc1h
        ld a,0ffh
        cp h
        jp z,sub_9ef5h

        ld (data_0a422),hl
        ld de,(data_a416)
        ld hl,00000h
        and a
        sbc hl,de
        push hl
        pop de
        ld hl,0ffech
        call 0bbc3h ;; gra move relative
        pop bc
        djnz l9f5ch
        ld hl,00000h
        ld de,00014h
        call 0bbc3h ;; gra move relative
        jp l9f0eh

        db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

;; screen dump to printer.
sub_0a028h:
        ld a,013h ;; INC instruction
        ld (la124),a
        ld a,00eh
        ld (la0e1+1),a
        ld a,(data_a133)
        bit 1,a
        jr z,la083h
        ld a,000h  ;; NOP instruction
        ld (la124),a
        ld a,007h
        ld (la0e1+1),a
la083h:
        call 0bd28h ;; mc reset printer

        ld de,00000h
        ld hl,00000h
        ld (data_a12d),de
sub_a050h:
        ld a,000h
        ld (data_a12c),a ;; accumulated pixels.

        ld de,(data_a12d)
        call sub_a10dh ;; store current graphics position

        call 0bbf0h ;; gra test absolute
        ld de,00001h
        call sub_a11dh ;; get pixel
        ld de,00002h
        call sub_a11dh ;; get pixel
        ld de,00004h
        call sub_a11dh ;; get pixel
        ld de,00008h
        call sub_a11dh ;; get pixel
        ld de,00010h
        call sub_a11dh ;; get pixel
        ld de,00020h
        call sub_a11dh ;; get pixel
        ld de,00040h
        call sub_a0ffh ;; get pixel
la0c9h:
        call 0bd2eh ;; mc busy printer
        jr c,la0c9h
        call 0bd28h ;; mc reset printer
        ld a,01bh    ;; ESC
        call 0bd2bh ;; mc print char
        ld a,04bh   ;; K
        call 0bd2bh ;; mc print char
        ld a,000h   ;; number of columns low byte
        call 0bd2bh ;; mc print char
        ld a,(data_a133)
        bit 0,a
        jr z,la0b4h
        ld a,002h   ;; number of columns high byte?
        call 0bd2bh ;; mc print char

        ld a,(data_a12c) ;; pixel data
        call 0bd2bh ;; mc print char
        jr la0f9h
la0b4h:
        ld a,001h   ;; number of columns high byte?
        call 0bd2bh ;; mc print char
la0f9h:
        ld a,(data_a12c) ;; pixel data
        call 0bd2bh ;; mc print char

        call sub_a115h
        inc hl
        inc hl
        call sub_a10dh
        ld a,h
        cp 001h
        jp nz,sub_a050h
        ld a,l
        cp 090h
        jp nz,sub_a050h
        ld a,00ah   ;; next line
        call 0bd2bh ;; mc print char

        ld a,02fh  ;; 47 - space
        call 0bb1eh ;; km test key
        ret nz
        ld hl,(data_a12d)
la0e1:
        ld de,0000eh
        add hl,de
        ld (data_a12d),hl
        ld hl,00000h
        call sub_a10dh
        ld de,(data_a12d)
        ld a,d
        cp 002h
        jp nz,sub_a050h
        ld a,e
        cp 084h
        jp nz,sub_a050h
        ret

;; accumulate pixels
sub_a0ffh:
        ld b,a

        call 0bb99h ;; txt get paper
        cp b        ;; was the colour read the same as the paper?
        ret z

        ;; any pixel which is not the same as the paper will be considered foreground
        ;; and the printer will draw a dot. Any pixel the same as the paper will
        ;; be considered background and no dot is drawn by the printer.

        ;; pixel is set to foreground. accumulate it into the pixel data so far
        ld hl,(data_a12c)
        add hl,de
        ld (data_a12c),hl
        ret

sub_a10dh:
        ld (data_a12f),hl
        ld (data_a131),de
        ret

sub_a115h:
        ld hl,(data_a12f)
        ld de,(data_a131)
        ret

sub_a11dh:
        call sub_a0ffh ;; accumulate a pixel for printing.
        call sub_a115h ;; get current X/Y graphics position
        inc de         ;; increment X to next pixel accross screen.
la124:
        inc de
        call sub_a10dh ;; set current X/Y graphics position
        call 0bbf0h ;; gra test absolute
        ret
data_a12c:
        db 00h
data_a12d:
        db 84h,02h
data_a12f:
        db 00h,00h
data_a131:
        dw 000eh
data_a133: 
        db 00h
data_a134:
        db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

sub_a154h:
        call 0bc11h ;; scr get mode
        cp 000h
        jr nz,la19dh
        ld b,004h
la19dh:
        cp 001h
        jr nz,la1a3h
        ld b,002h
la1a3h:
        cp 002h
        jr nz,la1a9h
        ld b,001h
la1a9h:
        ld a,b
la1aah:
        ld (data_a254),a
        call 0bbc6h ;; gra ask cursor
        ld (data_a250),hl
        ld (data_a250+2),de
la1b7h:
        call 0bb99h ;; txt get paper
        ld (pen_cmp+1),a
        ld bc,00000h
        ld (data_a24c),bc
        ld (data_a24e),bc
        call sub_a210
la18bh:
        ld a,02fh  ;; 47 - space
        call 0bb1eh ;; km test key
        ret nz
        ld hl,(data_a24e)
        inc hl
        inc hl
        ld a,004h
        cp h
        jr nz,la1deh
        ld hl,00000h
la1deh:
        ld (data_a24e),hl
        ld bc,05398h
        add hl,bc
        ld (buffer_addr3+2),hl ;; +2 to skip ED,53
buffer_addr3:
        ld de,(055e0h)
        ld hl,(data_a24e)
sub_a1efh:
        ld bc,057e4h
        add hl,bc
        ld (buffer_addr4+1),hl ;; +1 to skip 22
buffer_addr4:
        ld hl,(05a2ch)
        ld (data_a257),hl
        ld (data_a255),de
        ld bc,00000h
        ld a,(data_a254)
        ld c,a
        ex de,hl
        add hl,bc
        ex de,hl
        call sub_a1efh
        and a
        ex de,hl
        sbc hl,bc
        ex de,hl
        call sub_a1efh
        inc hl
        inc hl
        call sub_a1efh
        dec hl
        dec hl
        call sub_a1efh
        ld hl,(data_a24e)
        ld de,(data_a24c)
        inc hl
        inc hl
        and a
        sbc hl,de
        ret z
        jp la18bh

sub_a1efh:
        push de
        push hl
        push bc
        call 0bbf0h ;; gra test absolute
        pop bc
        pop hl
        pop de
pen_cmp:
        cp 000h
        jr nz,la248h
        ld (data_a250),hl
        ld (data_a252),de
        push bc
        call sub_0a210h
        pop bc
la248h:
        ld hl,(data_a257)
        ld de,(data_a255)
        ret
sub_a210:
        ld hl,(data_a250)
        ld de,(data_a252)
        call 0bbeah ;; gra plot absolute
        ld hl,(data_a24c)
        inc hl
        inc hl
        ld a,004h
        cp h
        jr nz,la267h
        ld hl,00000h
la267h:
        ld (data_a24c),hl
        ld bc,05398h
        add hl,bc
        ld de,(data_a252)
        ld (buffer_addr1+2),hl ;; +2 to skip ED, 53
buffer_addr1:
        ld (05648h),de
        ld hl,(data_a24c)
        ld bc,057e4h
        add hl,bc
        ld de,(data_a250)
        ld (buffer_addr+2),hl ;; +2 to skip ED, 53
buffer_addr2:
        ld (05a94h),de
        ret

;;Data area
data_a24c:
        defw 02b0h
data_a24e:
        defw 0248h
data_a250:
        defw 0beh
data_a252:
        defw 0120h
data_a254:
        defb 04h
data_a255:
        defw 0120h
data_a257:
        defw 0bch


;; detected light pen position
data_a410:
        defw 0
data_a412:
        defw 0
;; memory/buffer address
data_a414:
        defw 0
data_a416:
        defw 0
data_a418:
        defw 0

;; gra x,y position
data_a41a:
        defw 0
data_a41c: 
        defw 0
;; graphics pen
data_a41e:
        defb 0
        defb 0
data_a420:
        defw 0
        end