From 9a6188f821b11b69fff3d3a303dbfcce2e52e6f4 Mon Sep 17 00:00:00 2001 From: alekseiplusplus Date: Thu, 27 Jul 2023 11:36:01 +1000 Subject: refactoring of src/cpu/ --- src/cpu/6502.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/cpu/6502.c (limited to 'src/cpu/6502.c') diff --git a/src/cpu/6502.c b/src/cpu/6502.c new file mode 100644 index 0000000..d78cd7e --- /dev/null +++ b/src/cpu/6502.c @@ -0,0 +1,89 @@ +// 6502.h +// Main elements of the 6502 CPU + +#include"6502.h" + + +byte getFlag(byte flag) { + return ((P & flag) == flag) ? 1 : 0; +} + +void setFlag(byte flag, int x) { + if (x == 1){ + if ((P & flag) == 0x0) P += flag; + }else if (x == 0){ + if ((P & flag) == flag) P -= flag; + } + else{ + fprintf(stderr, "setFlag() passed arg neither 0 or 1"); + } +} + +void flagSet(byte flag){ + P |= flag; +} + +void flagClear(byte flag){ + P &= ~flag; +} + +// Functions which perform reusable routines for finding if a specific flag should be set. + +void setFlagN(byte x){ + if (x & flag_N == flag_N) + setFlag(flag_N, 1); + else + setFlag(flag_N, 0); +} + +//Perform prior to any changes +void setFlagV(byte x, byte y){ + if ((x & flag_N) == (y & flag_N)){ + if (((x + y) & (flag_N ^ 0xFF)) > 0x7F) setFlag(flag_V, 1); + else setFlag(flag_V, 0); + }else{ + if (((x - y) & (flag_N ^ 0xFF)) > 0x7F) setFlag(flag_V, 1); + else setFlag(flag_V, 0); + } +} + +/*void setFlagB(){ //WORK ON + setFlag(flag_B, 1); +}*/ + +/*void setFlagD(){ + setFlag(flag_D, 1); +}*/ + +/*void setFlagI(){ //WORK ON + setFlag(flag_Z, 1); +}*/ + +void setFlagZ(int x){ + if (x == 0) + setFlag(flag_Z, 1); + else + setFlag(flag_Z, 0); +} + +/*void setFlagC(){ + setFlag(flag_Z, 1); +}*/ + + +/*byte getMemory(address x){ + return Memory[x]; +} + +void setMemory(address x, byte y){ + Memory[x] = y; +}*/ + + +byte getStack() { + return getMemory(0x01FF - S); +} + +void setStack(byte z) { + setMemory(0x01FF - S, z); +} \ No newline at end of file -- cgit v1.2.3