gr-sim: work on getting a div by 7 working

This commit is contained in:
Vince Weaver 2018-09-08 23:52:58 -04:00
parent a06a08dbd9
commit f9e82a43d6
4 changed files with 167 additions and 2 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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

98
gr-sim/hgr/seven.c Normal file
View File

@ -0,0 +1,98 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#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;
}