From 39694d0aaa4bef354bf6c719d92d9c55757ee869 Mon Sep 17 00:00:00 2001 From: Greg King Date: Thu, 26 Nov 2015 15:06:20 -0500 Subject: [PATCH] Fixed bugs; and, improved the efficiency of some pce library functions. --- libsrc/pce/clock.s | 41 +++++++++------ libsrc/pce/clrscr.s | 13 ++--- libsrc/pce/color.s | 1 + libsrc/pce/conio.s | 101 ++++++++++++------------------------ libsrc/pce/cputc.s | 49 +++++++---------- libsrc/pce/crt0.s | 47 ++++++++--------- libsrc/pce/gotoxy.s | 7 --- libsrc/pce/irq.s | 4 +- libsrc/pce/joy/pce-stdjoy.s | 14 +++-- libsrc/pce/kplot.s | 21 +++----- libsrc/pce/psg.s | 12 ++--- libsrc/pce/revers.s | 27 +++++----- libsrc/pce/ticktock.s | 3 +- libsrc/pce/vce.s | 5 +- libsrc/pce/vdc.s | 34 +++++------- libsrc/pce/vga.inc | 6 ++- 16 files changed, 157 insertions(+), 228 deletions(-) diff --git a/libsrc/pce/clock.s b/libsrc/pce/clock.s index a57b6c4da..1d7579a2f 100644 --- a/libsrc/pce/clock.s +++ b/libsrc/pce/clock.s @@ -2,32 +2,39 @@ ; clock_t clock (void); ; - .export _clock .constructor initclock + .export _clock .forceimport ticktock ; make sure that tickcount changes - .importzp sreg - - .include "extzp.inc" - -.proc _clock - lda tickcount+3 - sta sreg+1 - lda tickcount+2 - sta sreg - ldx tickcount+1 - lda tickcount - rts -.endproc + .importzp tickcount, sreg ; Make the process clock start at zero. .segment "ONCE" initclock: - lda #0 - ldx #3 -@lp: sta tickcount,x + ldx #4 - 1 +@lp: stz tickcount,x dex bpl @lp rts + +; ------------------------------------------------------------------------ +.code + +; This function might be interrupted while it is reading the several bytes of +; the clock. They are read again if that happens. (We do not want to stop +; interrupts because that might cause glitches in interrupt-driven graphics +; and sound.) + +.proc _clock + lda tickcount + ldy tickcount+3 + sty sreg+1 + ldy tickcount+2 + sty sreg + ldx tickcount+1 + cmp tickcount + bne _clock ; clock changed; reread it + rts +.endproc diff --git a/libsrc/pce/clrscr.s b/libsrc/pce/clrscr.s index 907a78899..476277886 100644 --- a/libsrc/pce/clrscr.s +++ b/libsrc/pce/clrscr.s @@ -12,20 +12,15 @@ .include "extzp.inc" _clrscr: - st0 #VDC_MAWR - st1 #<$0000 - st2 #>$0000 - + VREG VDC_MAWR, $0000 st0 #VDC_VWR + ldy #$40 rowloop: ldx #$80 colloop: - lda #' ' ; low byte of char. index - sta VDC_DATA_LO - lda #$02 ; background color, high nybble of char. index - sta VDC_DATA_HI - + st1 #' ' ; low byte of char. index + st2 #$02 ; background color, high nybble of char. index dex bne colloop dey diff --git a/libsrc/pce/color.s b/libsrc/pce/color.s index 45eb9ffcd..a527cb2c0 100644 --- a/libsrc/pce/color.s +++ b/libsrc/pce/color.s @@ -21,6 +21,7 @@ _textcolor: rts _bgcolor: + and #$0F ldx BGCOLOR ; get old value sta BGCOLOR ; set new value asl a diff --git a/libsrc/pce/conio.s b/libsrc/pce/conio.s index 46907cc42..8fefe01f6 100644 --- a/libsrc/pce/conio.s +++ b/libsrc/pce/conio.s @@ -1,36 +1,27 @@ .constructor initconio, 24 - .import vce_init - .import psg_init .import vdc_init + .import psg_init .import colors .importzp ptr1, tmp1 .include "pce.inc" .include "extzp.inc" - .macpack longbranch .segment "ONCE" initconio: - jsr vce_init + jsr vdc_init jsr psg_init - jsr conio_init - jsr set_palette - - st0 #VDC_CR - st1 #<$0088 - st2 #>$0088 - rts + jsr load_font set_palette: stz VCE_ADDR_LO stz VCE_ADDR_HI - ldx #0 -@lp: - ldy #16 ; size of a pallette -@lp1: - lda colors,x + clx +@lp: ldy #16 ; size of a palette + +@lp1: lda colors,x sta VCE_DATA_LO lda colors+1,x sta VCE_DATA_HI @@ -39,58 +30,34 @@ set_palette: inx inx - cpx #16 * 2 - jne @lp + cpx #16 * 2 ; 16 palettes + bne @lp -; Set background to black. - - stz VCE_ADDR_LO - stz VCE_ADDR_HI - stz VCE_DATA_LO - stz VCE_DATA_HI + sty BGCOLOR ; white on black + iny + sty CHARCOLOR + VREG VDC_CR, $0088 ; enable background and vertical-blank interrupt rts -;---------------------------------------------------------------------------- -; The character tiles use only two colors from each pallette. Color zero -; comes from pallette zero; color one is different in each pallette. The -; color of a character is set by choosing one of the 16 pallettes. - -conio_init: - ; Load font - st0 #VDC_MAWR - st1 #<$2000 - st2 #>$2000 - - ; pointer to font data - lda #font - sta ptr1+1 +; Load the conio font into the VDC. +load_font: + VREG VDC_MAWR, $2000 + st0 #VDC_VWR - st0 #VDC_VWR ; VWR + stz tmp1 ; #%00000000 + bsr copy ; make normal characters - lda #0 - sta tmp1 - jsr copy + dec tmp1 ; #%11111111 +; bsr copy ; make reversed characters +; rts ; (fall through) - lda #font sta ptr1 - lda #>font - sta ptr1+1 - - lda #$FF - sta tmp1 - jsr copy - - ldx #0 ; white on black - stx BGCOLOR - inx - stx CHARCOLOR - - rts + stx ptr1+1 -copy: ldy #$80 ; 128 chars charloop: ldx #$08 ; 8 bytes/char @@ -98,23 +65,21 @@ lineloop: lda (ptr1) eor tmp1 sta VDC_DATA_LO ; bitplane 0 - stz VDC_DATA_HI ; bitplane 1 + st2 #>$0000 ; bitplane 1 - clc ; increment font pointer - lda ptr1 - adc #$01 - sta ptr1 - lda ptr1+1 - adc #$00 - sta ptr1+1 - dex + inc ptr1 ; increment font pointer + bne @noC + inc ptr1+1 +@noC: dex bne lineloop ; next bitplane-0 byte + ldx #$08 ; fill bitplanes 2 and 3 with 0 fillloop: st1 #<$0000 st2 #>$0000 dex bne fillloop ; next byte + dey bne charloop ; next character diff --git a/libsrc/pce/cputc.s b/libsrc/pce/cputc.s index 5d9afdc93..04d901423 100644 --- a/libsrc/pce/cputc.s +++ b/libsrc/pce/cputc.s @@ -5,6 +5,7 @@ .export _cputcxy, _cputc, cputdirect, putchar .export newline, plot + .forceimport initconio ; force conio initiation .import gotoxy .import PLOT @@ -23,9 +24,8 @@ _cputcxy: _cputc: cmp #$0D ; CR? bne L1 - lda #0 - sta CURS_X - beq plot ; Recalculate pointer + stz CURS_X + bra plot ; Recalculate pointer L1: cmp #$0A ; LF? beq newline ; Recalculate pointer @@ -35,20 +35,16 @@ L1: cmp #$0A ; LF? cputdirect: jsr putchar ; Write the character to the screen -; Advance cursor position +; Move the cursor (rightwards) to the next position. advance: ldy CURS_X iny cpy xsize bne L3 - jsr newline ; new line - ldy #0 ; + CR + inc CURS_Y ; new line + cly ; + CR L3: sty CURS_X - jmp plot - -newline: - inc CURS_Y ; Set cursor position; calculate VRAM pointer. @@ -57,24 +53,22 @@ plot: ldy CURS_X clc jmp PLOT ; Set the new cursor +newline: + inc CURS_Y + bra plot + ; Write one character to the screen without doing anything else. putchar: - ora RVS ; Set revers bit - - tax + ora RVS ; Set reverse bit - st0 #VDC_MAWR ; Memory Address Write + st0 #VDC_MAWR ; Memory-Address Write + ldy SCREEN_PTR + ldx SCREEN_PTR+1 + sty VDC_DATA_LO + stx VDC_DATA_HI - lda SCREEN_PTR - sta VDC_DATA_LO - - lda SCREEN_PTR+1 - sta VDC_DATA_HI - - st0 #VDC_VWR ; VWR - - txa + st0 #VDC_VWR sta VDC_DATA_LO ; character lda CHARCOLOR ; pallette number @@ -82,14 +76,7 @@ putchar: asl a asl a asl a - - ora #$02 + ora #>$0200 ; high nybble of char. index sta VDC_DATA_HI rts - -;------------------------------------------------------------------------------- -; force the init constructor to be imported - - .import initconio -conio_init = initconio diff --git a/libsrc/pce/crt0.s b/libsrc/pce/crt0.s index 2b079727f..d6dee3ef4 100644 --- a/libsrc/pce/crt0.s +++ b/libsrc/pce/crt0.s @@ -1,18 +1,19 @@ ; -; Startup code for cc65 (PCEngine version) +; Start-up code for cc65 (PC-Engine version) ; ; by Groepaz/Hitmen , -; based on code by Ullrich von Bassewitz . +; based on code by Ullrich von Bassewitz ; -; 2018-02-11, Greg King +; 2018-02-24, Greg King ; .export _exit - .export __STARTUP__ : absolute = 1 ; Mark as startup + .export __STARTUP__ : absolute = 1 ; Mark as start-up .import initlib, donelib .import push0, _main - .import IRQStub + .import IRQStub, __nmi + .importzp sp ; Linker-generated .import __CARTSIZE__ @@ -23,20 +24,18 @@ .include "pce.inc" .include "extzp.inc" - .importzp sp - ; ------------------------------------------------------------------------ -; Place the startup code in a special segment. +; Place the start-up code in a special segment. .segment "STARTUP" - ; Initialize CPU + ; Initialize the CPU. start: sei nop csh ; Set high-speed CPU mode nop - ; Set up stack and memory mapping. + ; Set up the stack and the memory mapping. ldx #$FF ; Stack top ($21FF) txs @@ -62,18 +61,19 @@ start: sei ;lda #$00 ; (The reset default) ;tam #%10000000 ; $E000-$FFFF Hucard/Syscard bank 0 - ; Initialize hardware + ; Initialize the hardware. stz TIMER_CTRL ; Timer off - lda #$07 + lda #%00000111 sta IRQ_MASK ; Interrupts off - stz IRQ_STATUS ; Acknowledge timer - ; FIXME; I don't know why the heck this one doesn't work when called from a constructor. :/ + ; FIXME; I don't know why the heck this one doesn't work when called from a constructor. -Groepaz :-/ +.if 0 ; It now seems to work (at least, in Mednafen). -Greg King .import vdc_init jsr vdc_init +.endif - ; Turn on background and VD interrupt/IRQ1 - lda #$05 + ; Allow interrupts from the VDC. + lda #%00000101 sta IRQ_MASK ; IRQ1 = on ; Copy the .data segment to RAM @@ -89,10 +89,11 @@ start: sei sta sp stx sp+1 - ; Call module constructors + ; Call the module constructors. jsr initlib - cli ; allow IRQ only after constructors have run + stz IRQ_STATUS ; Clear IRQs + cli ; Allow IRQ only after constructors have run ; Pass an empty command line jsr push0 ; argc @@ -101,14 +102,12 @@ start: sei ldy #4 ; Argument size jsr _main ; Call the user's code - ; Call module destructors. This is also the _exit entry. -_exit: jsr donelib ; Run module destructors + ; Call the module destructors. This is also the exit() entry. +_exit: jsr donelib - ; reset the PCEngine (start over) + ; Reset the PCEngine (start over). jmp start -_nmi: rti - .export initmainargs initmainargs: rts @@ -121,5 +120,5 @@ initmainargs: .word IRQStub ; $FFF6 IRQ2 (External IRQ, BRK) .word IRQStub ; $FFF8 IRQ1 (VDC) .word IRQStub ; $FFFA Timer - .word _nmi ; $FFFC NMI + .word __nmi ; $FFFC NMI .word start ; $FFFE reset diff --git a/libsrc/pce/gotoxy.s b/libsrc/pce/gotoxy.s index dd7cbf1c2..49f0c602a 100644 --- a/libsrc/pce/gotoxy.s +++ b/libsrc/pce/gotoxy.s @@ -17,10 +17,3 @@ _gotoxy: jsr popa ; Get X sta CURS_X ; Set X jmp plot ; Set the cursor position - -;------------------------------------------------------------------------------- -; force the init constructor to be imported - - .import initconio -conio_init = initconio - diff --git a/libsrc/pce/irq.s b/libsrc/pce/irq.s index 60a7e22ba..7ab42d8a8 100644 --- a/libsrc/pce/irq.s +++ b/libsrc/pce/irq.s @@ -2,7 +2,7 @@ ; IRQ handling (PCE version) ; - .export initirq, doneirq, IRQStub + .export initirq, doneirq, IRQStub, __nmi .import __INTERRUPTOR_COUNT__, callirq_y @@ -45,4 +45,4 @@ IRQStub: pla plx @L1: ply - rti +__nmi: rti diff --git a/libsrc/pce/joy/pce-stdjoy.s b/libsrc/pce/joy/pce-stdjoy.s index c3985b203..2de3d0c4c 100644 --- a/libsrc/pce/joy/pce-stdjoy.s +++ b/libsrc/pce/joy/pce-stdjoy.s @@ -36,6 +36,10 @@ JOY_COUNT = 4 ; Number of joysticks we support +.bss + +padbuffer: .res JOY_COUNT + .code ; ------------------------------------------------------------------------ @@ -67,7 +71,7 @@ UNINSTALL: COUNT: lda #JOY_COUNT + clx ; ldx #>JOY_COUNT rts ; ------------------------------------------------------------------------ @@ -85,7 +89,6 @@ READJOY: joy1: lda padbuffer,x - ldx #0 rts read_joy: @@ -134,11 +137,6 @@ nextpad: sta padbuffer,y ; store new value iny - cpy #$05 + cpy #.sizeof(padbuffer) bcc nextpad rts - -.bss - -padbuffer: - .res 4 diff --git a/libsrc/pce/kplot.s b/libsrc/pce/kplot.s index b533e4a2a..ae31710f3 100644 --- a/libsrc/pce/kplot.s +++ b/libsrc/pce/kplot.s @@ -4,16 +4,15 @@ .include "pce.inc" .include "extzp.inc" -PLOT: - bcs @getpos +PLOT: bcs @getpos tya ;clc ; already cleared - adc _plotlo,x + adc plotlo,x sta SCREEN_PTR - lda _plothi,x - adc #0 + cla + adc plothi,x sta SCREEN_PTR+1 @getpos: ldx CURS_Y @@ -22,18 +21,10 @@ PLOT: .rodata -_plotlo: - .repeat screenrows,line +plotlo: .repeat screenrows,line .byte <($0000+(line*$80)) .endrepeat -_plothi: - .repeat screenrows,line +plothi: .repeat screenrows,line .byte >($0000+(line*$80)) .endrepeat - -;------------------------------------------------------------------------------- -; force the init constructor to be imported - - .import initconio -conio_init = initconio diff --git a/libsrc/pce/psg.s b/libsrc/pce/psg.s index 996b4e5ac..7087c2084 100644 --- a/libsrc/pce/psg.s +++ b/libsrc/pce/psg.s @@ -4,8 +4,8 @@ .segment "ONCE" psg_init: - clx stz PSG_GLOBAL_PAN ; Silence global balance + ldx #6 - 1 psg_clear_loop: stx PSG_CHAN_SELECT ; Select channel @@ -17,14 +17,12 @@ psg_clear_loop: stz PSG_LFO_FREQ ; Clear LFO frequency stz PSG_LFO_CTRL ; Clear LFO control - cly + ldy #$20 psg_clear_waveform: stz PSG_CHAN_DATA ; Clear waveform byte - iny - cpy #$20 + dey bne psg_clear_waveform - inx - cpx #$06 - bne psg_clear_loop + dex + bpl psg_clear_loop rts diff --git a/libsrc/pce/revers.s b/libsrc/pce/revers.s index b31ff150c..ff9957efd 100644 --- a/libsrc/pce/revers.s +++ b/libsrc/pce/revers.s @@ -1,24 +1,23 @@ ; -; Ullrich von Bassewitz, 07.08.1998 +; 1998-08-07, Ullrich von Bassewitz +; 2015-11-23, Greg King ; -; unsigned char revers (unsigned char onoff); +; unsigned char __fastcall__ revers (unsigned char onoff); ; .export _revers - .include "extzp.inc" + .importzp RVS .proc _revers - ldx #$00 ; Assume revers off - tay ; Test onoff - beq L1 ; Jump if off - ldx #$80 ; Load on value - ldy #$00 ; Assume old value is zero -L1: lda RVS ; Load old value - stx RVS ; Set new value - beq L2 ; Jump if old value zero - iny ; Make old value = 1 -L2: ldx #$00 ; Load high byte of result - tya ; Load low byte, set CC + cmp #$01 ; False or true? + cla + ror a ; Either $00 or $80 + ldy RVS ; Load old value + sta RVS ; Set new value + tya + asl a + rol a ; Either $00 or $01 + clx rts .endproc diff --git a/libsrc/pce/ticktock.s b/libsrc/pce/ticktock.s index 433e00e8d..b1974ee4d 100644 --- a/libsrc/pce/ticktock.s +++ b/libsrc/pce/ticktock.s @@ -1,6 +1,5 @@ - .interruptor ticktock, 24 + .interruptor ticktock - .include "pce.inc" .include "extzp.inc" ticktock: diff --git a/libsrc/pce/vce.s b/libsrc/pce/vce.s index 4a498ab58..d7f5bdc1d 100644 --- a/libsrc/pce/vce.s +++ b/libsrc/pce/vce.s @@ -7,9 +7,10 @@ vce_init: ; Set CTA to zero stz VCE_ADDR_LO stz VCE_ADDR_HI - ldy #$01 + + ldy #$01 ; Only background palettes vce_clear_bank: - ldx #$00 + clx ; ldx #<$0100 ; <(16 * 16) vce_clear_color: stz VCE_DATA_LO ; Clear color (LSB) stz VCE_DATA_HI ; Clear color (MSB) diff --git a/libsrc/pce/vdc.s b/libsrc/pce/vdc.s index e73004db8..8495a9163 100644 --- a/libsrc/pce/vdc.s +++ b/libsrc/pce/vdc.s @@ -8,33 +8,25 @@ HIRES = 1 vdc_init: lda VDC_CTRL - VREG $00, $0000 ; MAWR - VREG $01, $0000 ; MARR - VREG $05, $0000 ; CR - VREG $06, $0000 ; RCR - VREG $07, $0000 ; BXR - VREG $08, $0000 ; BYR - VREG $09, $0070 ; MWR - VREG $0C, $1702 ; CRTC - VSR - VREG $0D, $00DF ; CRTC - VDS - VREG $0E, $000C ; CRTC - VDE - VREG $0F, $0000 ; DCR + VREG VDC_CR , $0000 ; disable display and interrupts + VREG VDC_BXR, $0000 ; no scrolling + VREG VDC_BYR, $0000 + VREG VDC_MWR, $0070 ; 128 x 64 tiles (1024 x 512 pixels) + VREG VDC_VSR, $1702 ; CRTC + VREG VDC_VDR, $00DF ; CRTC - VDS + VREG VDC_VCR, $000C ; CRTC - VDE + VREG VDC_DCR, $0000 .if HIRES - - VREG $0A, $0C02 ; CRTC - HSR - VREG $0B, $043C ; CRTC - HDS + VREG VDC_HSR, $0C02 ; CRTC + VREG VDC_HDR, $043C ; CRTC - HDS lda #$06 - sta VCE_CTRL - .else - - VREG $0A, $0202 ; CRTC - HSR - VREG $0B, $041F ; CRTC - HDS + VREG VDC_HSR, $0202 ; CRTC + VREG VDC_HDR, $041F ; CRTC - HDS lda #$04 - sta VCE_CTRL - .endif + sta VCE_CTRL lda VDC_CTRL rts diff --git a/libsrc/pce/vga.inc b/libsrc/pce/vga.inc index 9317d6ce4..72c173146 100644 --- a/libsrc/pce/vga.inc +++ b/libsrc/pce/vga.inc @@ -1,5 +1,9 @@ +;---------------------------------------------------------------------------- +; VGA font for the PC-Engine conio implementation -; VGA charset for the PC-Engine conio implementation +; The character tiles use only two colors from each pallette. Color zero +; comes from pallette zero; color one is different in each pallette. The +; color of a character is set by choosing one of the 16 pallettes. .byte $00, $00, $00, $00, $00, $00, $00, $00 -- 2.39.5