Bug fix: manage r12 correctly, and TXS used a wrong register

To use r12 in assembly code:
 - use -fomit-frame-pointer for test.c
 - save and restore on calling libc functions
This commit is contained in:
Takashi Toyoshima 2014-12-07 16:35:46 +09:00
parent dbf8142e61
commit 91c94746d8
3 changed files with 43 additions and 6 deletions

31
6502.S
View File

@ -12,9 +12,8 @@
.extern prn
.macro prn reg
push {r0-r3}
mov r0, \reg
bl prn
pop {r0-r3}
bl prn
pop {r0-r3}
.endm
#define T0 r4
@ -36,7 +35,18 @@
#define FLAG_C (1 << 0)
.macro _ldb
.ifdef USE_FRAMEPOINTER
// If -fomit-frame-pointer is not specified, save and restore r12 here.
push {r1}
mov r1, r12
push {r1}
.endif
bl cpu6502_load
.ifdef USE_FRAMEPOINTER
pop {r1}
mov r12, r1
pop {r1}
.endif
.endm
.macro _ldw
@ -50,7 +60,17 @@
.endm
.macro _stb
.ifdef USE_FRAMEPOINTER
push {r1}
mov r1, r12
push {r1}
.endif
bl cpu6502_store
.ifdef USE_FRAMEPOINTER
pop {r1}
mov r12, r1
pop {r1}
.endif
.endm
.macro _stw
@ -1244,7 +1264,10 @@ op99: // STA - Absolute, Y
_decode
op9a: // TXS
_t RX, SR
_t RX, SP
movs r0, #1
lsls r0, r0, #8
add SP, SP, r0
_decode
op9d: // STA - Absolute, X

View File

@ -13,6 +13,8 @@ CLOCK = 12000
ROM = applebasic
ROMOBJ = $(ROM).o
ROMFLAG = -I binary -O elf32-littlearm -B arm --rename-section .data=.rodata
FRAMEPTR= -fomit-frame-pointer
#FRAMEPTR= -DUSE_FRAMEPOINTER
OBJS = vectors.o reset.o 6502.o apple2.o uart.o $(ROMOBJ)
$(APP).bin: $(APP)
@ -37,8 +39,8 @@ $(ROM).rom:
--strip-symbol _binary_$(ROM)_rom_size
# Test binary that runs on qemu user mode emulation for testing
test: 6502.o test.c
$(CC) -mthumb -static test.c 6502.o -o test && qemu-arm test
test: 6502.S test.c
$(CC) -mthumb -static $(FRAMEPTR) test.c 6502.S -o test && qemu-arm test
# Assume a CQ Mary comaptible board.
run: $(APP).bin

12
test.c
View File

@ -5,27 +5,39 @@
uint8_t mem[0x10000];
void prn(int c) {
uint8_t fp;
__asm__("movs %0, r12": "=r"(fp));
printf("### $%02x ###\n", c);
__asm__("movs r12, %0":: "r"(fp));
}
void cpu6502_dump(
uint16_t pc, uint8_t a, uint8_t x, uint8_t y, uint8_t sp, uint8_t sr) {
uint8_t fp;
__asm__("movs %0, r12": "=r"(fp));
printf("*** dump *** PC=$%04x A=$%02x X=$%02x Y=$%02x SP=$%02x "
"NV-B_DIZC=%d%d-%d_%d%d%d%d\n",
pc, a, x, y, sp, (sr >> 7) & 1, (sr >> 6) & 1, (sr >> 4) & 1,
(sr >> 3) & 1, (sr >> 2) & 1, (sr >> 1) & 1, sr & 1);
fflush(stdout);
__asm__("movs r12, %0":: "r"(fp));
}
uint8_t cpu6502_load(uint16_t addr) {
uint8_t fp;
__asm__("movs %0, r12": "=r"(fp));
printf("load $%04x => $%02x\n", addr, mem[addr]);
fflush(stdout);
__asm__("movs r12, %0":: "r"(fp));
return mem[addr];
}
void cpu6502_store(uint16_t addr, uint8_t data) {
uint8_t fp;
__asm__("movs %0, r12": "=r"(fp));
printf("store $%04x <= $%02x\n", addr, data);
fflush(stdout);
__asm__("movs r12, %0":: "r"(fp));
mem[addr] = data;
}