From f9e82a43d6b58149f234dc1544c8cfee32cf3310 Mon Sep 17 00:00:00 2001 From: Vince Weaver Date: Sat, 8 Sep 2018 23:52:58 -0400 Subject: [PATCH] gr-sim: work on getting a div by 7 working --- gr-sim/6502_emulate.c | 57 +++++++++++++++++++++++++ gr-sim/6502_emulate.h | 3 ++ gr-sim/hgr/Makefile | 11 ++++- gr-sim/hgr/seven.c | 98 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 gr-sim/hgr/seven.c diff --git a/gr-sim/6502_emulate.c b/gr-sim/6502_emulate.c index 1bc51d86..7dbe8bb3 100644 --- a/gr-sim/6502_emulate.c +++ b/gr-sim/6502_emulate.c @@ -62,6 +62,27 @@ void adc(int value) { } +void adc_mem(int addr) { + + int temp_a; + int temp_value; + int result; + + temp_a=a&0xff; + temp_value=ram[addr]&0xff; + + result=(temp_a+temp_value+c); + + c=(result&0x100)>>8; + n=(result&0x80)>>7; + + v=!!((a^result)&(temp_value^result)&0x80); + + a=result&0xff; + z=(a==0); + +} + void sbc(int value) { int temp_a; int result; @@ -83,6 +104,29 @@ void sbc(int value) { z=(a==0); +} + +void sbc_mem(int addr) { + int temp_a; + int result; + int temp_value; + + temp_a=a&0xff; + temp_value=(~ram[addr])&0xff; + + result=temp_a+temp_value+c; + +// printf("SBC: %x - %x (%x) = %x\n",a,value,c,result); + + c=(result&0x100)>>8; + n=(result&0x80)>>7; + + v=!!((a^result)&((255-ram[addr])^result)&0x80); + + a=result&0xff; + z=(a==0); + + } void cmp(int value) { @@ -366,6 +410,19 @@ void ldy_const(int value) { n=!!(y&0x80); } +void sta(int addr) { + + ram[addr]=a; +} + +void tax(void) { + x=a; +} + +void txa(void) { + a=x; +} + unsigned char high(int value) { return (value>>8)&0xff; } diff --git a/gr-sim/6502_emulate.h b/gr-sim/6502_emulate.h index cbbc01fe..78f012ce 100644 --- a/gr-sim/6502_emulate.h +++ b/gr-sim/6502_emulate.h @@ -17,6 +17,8 @@ void lsr(void); void asl(void); void ror(void); void rol(void); +void adc_mem(int addr); +void sbc_mem(int addr); void asl_mem(int addr); void ror_mem(int addr); void rol_mem(int addr); @@ -36,6 +38,7 @@ void ldx(int addr); void ldx_const(int value); void ldy(int addr); void ldy_const(int value); +void sta(int addr); unsigned char high(int value); unsigned char low(int value); diff --git a/gr-sim/hgr/Makefile b/gr-sim/hgr/Makefile index c4acda29..bc40ee0d 100644 --- a/gr-sim/hgr/Makefile +++ b/gr-sim/hgr/Makefile @@ -6,7 +6,7 @@ SDL_LIBS= `sdl-config --libs` SDL_INCLUDE= `sdl-config --cflags` GR_SIM = ../gr-sim.a -all: fireworks fw_purple lines image_load hgr_view +all: fireworks fw_purple lines image_load hgr_view seven ### @@ -47,9 +47,16 @@ fw_purple: fw_purple.o $(GR_SIM) fw_purple.o: fw_purple.c $(CC) $(CFLAGS) -c fw_purple.c +### + +seven: seven.o $(GR_SIM) + $(CC) $(LFLAGS) $(SDL_LIBS) -o seven seven.o $(GR_SIM) + +seven.o: seven.c + $(CC) $(CFLAGS) -c seven.c #### clean: - rm -f *~ *.o fireworks lines image_load hgr_view fw_purple + rm -f *~ *.o fireworks lines image_load hgr_view fw_purple seven diff --git a/gr-sim/hgr/seven.c b/gr-sim/hgr/seven.c new file mode 100644 index 00000000..671585a3 --- /dev/null +++ b/gr-sim/hgr/seven.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include + +#include "6502_emulate.h" +#include "gr-sim.h" + +#define TEMP_Q 0xff +#define TEMP_R 0xfe + +static void fancy_div(int d, int *q, int *r) { + + //;Divide by 7 (From December '84 Apple Assembly Line) + //;15 bytes, 27 cycles + + // y=xhigh x=xlow a=?? + // q in y, r in x + + y=(d>>8)&0xff; + x=d&0xff; + + a=x; + + sta(TEMP_R); + + c=0; + sta(TEMP_Q); // 0 + lsr(); // 0 + lsr(); // 0 + lsr(); // 0 + adc_mem(TEMP_Q); // 0 + ror(); // 0 + lsr(); // 0 + lsr(); // 0 + adc_mem(TEMP_Q); // 0 + ror(); // 0 + lsr(); // 0 + lsr(); // 0 + + c=0; + sta(TEMP_Q); + asl(); + adc_mem(TEMP_Q); + asl(); + adc_mem(TEMP_Q); + + c=1; + sta(TEMP_R); + txa(); + sbc_mem(TEMP_R); + tax(); + + if (y) { + x+=4; + lda(TEMP_Q); + c=0; + adc(36); + sta(TEMP_Q); + } + + y=ram[TEMP_Q]; + + if (x>6) { + c=1; + txa(); + sbc(7); + tax(); + y++; + } + + *q=y; + *r=x; +} + + +int main(int argc, char **argv) { + + int i,actual_q,actual_r; + int fancy_q,fancy_r; + + grsim_init(); + + for(i=0;i<280;i++) { + + fancy_div(i,&fancy_q,&fancy_r); + + actual_q=i/7; + actual_r=i%7; + + if ((fancy_q!=actual_q) || (fancy_r!=actual_r)) { + printf("%03x\t%d,%d\t%d,%d\n", + i,actual_q,actual_r,fancy_q,fancy_r); + } + } + + return 0; +}