From 19d02d1eed0886220e64f4ed87bc8da8666f8760 Mon Sep 17 00:00:00 2001 From: alekseiplusplus Date: Mon, 27 Mar 2023 12:56:11 +1100 Subject: Added instructions, changed C and V set flag funcs --- applesystem.h | 24 +++++++++++++++++------- instruction.h | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/applesystem.h b/applesystem.h index a3a33f5..ea9b322 100644 --- a/applesystem.h +++ b/applesystem.h @@ -2,6 +2,12 @@ typedef unsigned char byte; byte acc, X, Y, S, P = 0x00; byte Memory[4096]; // TO DO. Add expansion capability to memory. +/* +To Do. + + Find variables better passed as pointers instead +*/ + + // FLAGS const byte flag_N = 0x80; // Negative (Note that byte cast is necessary here only) const byte flag_V = 0x40; // Overflow @@ -65,8 +71,9 @@ Zero Page ADC $44 $65 2 3 instruction(0x65); -> ADC({enum for Zero Page}); */ // Set particular flags -void setFlagN(){ - if (acc ^ 0x80){ +// Functions which quickly set specific flags that are straightforward checks, and are repeated often. +void setFlagN(byte x){ + if (x ^ flag_N == flag_N){ setFlag(flag_N, 1); }else{ //not sure if this should be present, I think it is setFlag(flag_N, 0); @@ -74,11 +81,13 @@ void setFlagN(){ } //Perform prior to any changes -void setFlagV(byte x, byte y){ //This is pathetic. +void setFlagV(byte x, byte y){ if ((x & flag_N) == (y & flag_N)){ - if (((x + y) & (flag_N ^ 0xFF)) > 0x8F) setFlag(flag_V, 1); + if (((x + y) & (flag_N ^ 0xFF)) > 0x7F) setFlag(flag_V, 1); + else setFlag(flag_V, 0); }else{ - if (((x - y) & (flag_N ^ 0xFF)) > 0x8F) setFlag(flag_V, 1); + if (((x - y) & (flag_N ^ 0xFF)) > 0x7F) setFlag(flag_V, 1); + else setFlag(flag_V, 0); } } /*void setFlagB(){ //WORK ON @@ -95,9 +104,10 @@ void setFlagZ(int x){ setFlag(flag_Z, 1); } } -void setFlagC(){ // NOTE. Must make setFlagC functional with independence. Look into carrying on 6502 +//Only 6 instructions, 2 not including stack instructions, use the carry flag. +/*void setFlagC(){ // NOTE. Must make setFlagC functional with independence. Look into carrying on 6502 setFlag(flag_Z, 1); -} +}*/ diff --git a/instruction.h b/instruction.h index af48ba0..054e629 100644 --- a/instruction.h +++ b/instruction.h @@ -27,9 +27,36 @@ void fSTY(Addressing addr, int val){ Memory[(fAddressing(addr, val))] = Y; } +// Arithmetic Instructions +void fADC(Addressing addr, int val){ + int buffer = acc + fAddressing(addr, val); + setFlagV(buffer, acc); -// Arithmetic Instructions + if (buffer > 255){ + setFlag(flag_C, 1); + }else{ + setFlag(flag_C, 0); + } + + acc += fAddressing(addr, val); + setFlagN(acc); + setFlagZ(acc); +} + +void fSBC(Addressing addr, int val){ + int buffer = acc - fAddressing(addr, val); + setFlagV(buffer, acc); + + if (buffer < 0){ + setFlag(flag_C, 1); + }else{ + setFlag(flag_C, 0); + } + acc -= fAddressing(addr, val); + setFlagN(acc); + setFlagZ(acc); +} //Increment and Decrement Instructions @@ -52,7 +79,7 @@ void fORA(Addressing addr, int val){ void fEOR(Addressing addr, int val){ acc = acc ^ fAddressing(addr, val); - setFlagN(); + setFlagN(acc); setFlagZ(acc); } @@ -67,7 +94,7 @@ void fEOR(Addressing addr, int val){ void fASL(Addressing addr, int val){ setFlag(flag_C, (val & 0x80)); acc = (val << 1); - setFlagN(); + setFlagN(acc); setFlagZ(acc); } -- cgit v1.2.3