From e89c0e85ddc321aa926077194c794fdf540715f9 Mon Sep 17 00:00:00 2001 From: Vince Weaver Date: Sun, 9 Sep 2018 15:54:53 -0400 Subject: [PATCH] hgr: more work on constant-time hplot --- fireworks/hgr.s | 111 +++++++++++++++++++++++++++++++++++------- gr-sim/6502_emulate.c | 18 +++++++ gr-sim/6502_emulate.h | 2 + gr-sim/hgr/seven.c | 54 +++++++++++--------- 4 files changed, 146 insertions(+), 39 deletions(-) diff --git a/fireworks/hgr.s b/fireworks/hgr.s index 11090019..eca9997e 100644 --- a/fireworks/hgr.s +++ b/fireworks/hgr.s @@ -80,13 +80,19 @@ bkgnd_loop: -msktbl: .byte $81,$82,$84,$88,$90,$A0,$C0 +msktbl: .byte $81,$82,$84,$88,$90,$A0,$C0 ; original + hposn: ; F411 + ; move into expected zp locations sta HGR_Y ; 3 stx HGR_X ; 3 sty HGR_X+1 ; 3 + ;=========== + ; 9 + + ; calc y-position addr. no lookup table? pha ; 3 and #$C0 ; 2 sta GBASL ; 3 @@ -95,9 +101,6 @@ hposn: ora GBASL ; 3 sta GBASL ; 3 pla ; 4 - ;=========== - ; 31 - ; F423 sta GBASH ; 3 asl ; 2 asl ; 2 @@ -112,24 +115,98 @@ hposn: ora HGR_PAGE ; 3 sta GBASH ; 3 ;============ - ; 39 + ; 61 ; F438 - txa ; 2 - cpy #0 ; 2 - beq hposn_2 ; 3 + ; divide/mod 16-bit x poisition by 7 + ; incoming, X=(y,x) + ; outgoing y=q, a=r + + ; Divide by 7 (From December '84 Apple Assembly Line) + + txa + clc + sta HGR_HORIZ + lsr + lsr + lsr + adc HGR_HORIZ + ror + lsr + lsr + adc HGR_HORIZ + ror + lsr + lsr + ; x/7 is in A + + ; calculate remainder + clc + sta HGR_HORIZ + asl + adc HGR_HORIZ + asl + adc HGR_HORIZ + ; HGR_HORIZ=x/7, A=HGR_HORIZ*7 + + ; calculate remainder by X-(Q*7) + sec + eor #$ff + adc HGR_X + ; A = remainder + + cpy #0 + beq only_low +theres_high: + + clc + adc #4 ; make remainder match + pha + lda HGR_HORIZ + adc #36 + sta HGR_HORIZ + pla +only_low: + cmp #7 + bcc done_mod ; blt + + sec + sbc #7 + inc HGR_HORIZ + +done_mod: + + +; txa ; 2 +; cpy #0 ; 2 +; beq hposn_2 ; 3 ; -1 - ldy #35 ; 2 - adc #4 ; 2 -hposn_1: - iny ; 2 +; ldy #35 ; 2 +; adc #4 ; 2 +;hposn_1: +; iny ; 2 ; f442 -hposn_2: - sbc #7 ; 2 - bcs hposn_1 ; 3 +;hposn_2: +; sbc #7 ; 2 +; bcs hposn_1 ; 3 ; -1 - sty HGR_HORIZ ; 3 + ldy HGR_HORIZ +; sty HGR_HORIZ ; 3 tax ; 2 - lda msktbl-$100+7,x ; LDA MSKTBL-$100+7,X BIT MASK ; 4? + lda msktbl,x ; +; lda msktbl-$100+7,x ; LDA MSKTBL-$100+7,X BIT MASK ; 4? + ; weird two's complement? + + ; if x=-6 = 249 = fa + ; F5b2 - 1000 = f4b2 + ; f4b2+7 = f4b9 + ; -1 ff + f4b9 = f5b8 C0 6 + ; -2 fe + f4b9 = f5b7 A0 5 + ; -3 fd + f4b9 = f5b6 90 4 + ; -4 fc + f4b9 = f5b5 88 3 + ; -5 fb + f4b9 = f5b4 84 2 + ; -6 fa + f4b9 = f5b3 82 1 + ; -7 f9 + f4b9 = f5b2 81 0 + ; MSKTBL=F5B2 sta HMASK ; 3 tya ; 2 diff --git a/gr-sim/6502_emulate.c b/gr-sim/6502_emulate.c index 7dbe8bb3..612f3287 100644 --- a/gr-sim/6502_emulate.c +++ b/gr-sim/6502_emulate.c @@ -423,6 +423,24 @@ void txa(void) { a=x; } +void eor(int value) { + + int temp_a; + int temp_value; + int result; + + temp_a=a&0xff; + temp_value=value&0xff; + + result=(temp_a^temp_value); + + n=(result&0x80)>>7; + + a=result&0xff; + z=(a==0); + +} + unsigned char high(int value) { return (value>>8)&0xff; } diff --git a/gr-sim/6502_emulate.h b/gr-sim/6502_emulate.h index 78f012ce..4f116050 100644 --- a/gr-sim/6502_emulate.h +++ b/gr-sim/6502_emulate.h @@ -8,6 +8,7 @@ unsigned short y_indirect(unsigned char base, unsigned char y); int init_6502(void); void adc(int value); void sbc(int value); +void eor(int value); void cmp(int value); void cpy(int value); void cpx(int value); @@ -39,6 +40,7 @@ void ldx_const(int value); void ldy(int addr); void ldy_const(int value); void sta(int addr); +void inc_mem(int addr); unsigned char high(int value); unsigned char low(int value); diff --git a/gr-sim/hgr/seven.c b/gr-sim/hgr/seven.c index 671585a3..ef766bcd 100644 --- a/gr-sim/hgr/seven.c +++ b/gr-sim/hgr/seven.c @@ -6,7 +6,7 @@ #include "6502_emulate.h" #include "gr-sim.h" -#define TEMP_Q 0xff +#define HGR_HORIZ 0xff #define TEMP_R 0xfe static void fancy_div(int d, int *q, int *r) { @@ -15,7 +15,7 @@ static void fancy_div(int d, int *q, int *r) { //;15 bytes, 27 cycles // y=xhigh x=xlow a=?? - // q in y, r in x + // q in y, r in a y=(d>>8)&0xff; x=d&0xff; @@ -25,52 +25,62 @@ static void fancy_div(int d, int *q, int *r) { sta(TEMP_R); c=0; - sta(TEMP_Q); // 0 + sta(HGR_HORIZ); // 0 lsr(); // 0 lsr(); // 0 lsr(); // 0 - adc_mem(TEMP_Q); // 0 + adc_mem(HGR_HORIZ); // 0 ror(); // 0 lsr(); // 0 lsr(); // 0 - adc_mem(TEMP_Q); // 0 + adc_mem(HGR_HORIZ); // 0 ror(); // 0 lsr(); // 0 lsr(); // 0 + // calc remainder + c=0; - sta(TEMP_Q); + sta(HGR_HORIZ); asl(); - adc_mem(TEMP_Q); + adc_mem(HGR_HORIZ); asl(); - adc_mem(TEMP_Q); + adc_mem(HGR_HORIZ); + // HGR_HORIZ=x/7, A=HGR_HORIZ*7 + c=1; - sta(TEMP_R); - txa(); - sbc_mem(TEMP_R); - tax(); + eor(0xff); +// printf("%d+%d=",d&0xff,a); + adc(d&0xff); +// printf("%d\n",a); + + +// sta(TEMP_R); +// txa(); +// sbc_mem(TEMP_R); +// tax(); if (y) { - x+=4; - lda(TEMP_Q); c=0; + adc(4); + pha(); + lda(HGR_HORIZ); adc(36); - sta(TEMP_Q); + sta(HGR_HORIZ); + pla(); } - y=ram[TEMP_Q]; - - if (x>6) { + if (a>6) { c=1; - txa(); sbc(7); - tax(); - y++; + ram[HGR_HORIZ]++; } + y=ram[HGR_HORIZ]; + *q=y; - *r=x; + *r=a; }