summaryrefslogtreecommitdiff
path: root/applesystem.h
diff options
context:
space:
mode:
Diffstat (limited to 'applesystem.h')
-rw-r--r--applesystem.h107
1 files changed, 107 insertions, 0 deletions
diff --git a/applesystem.h b/applesystem.h
new file mode 100644
index 0000000..a3a33f5
--- /dev/null
+++ b/applesystem.h
@@ -0,0 +1,107 @@
+typedef unsigned char byte;
+byte acc, X, Y, S, P = 0x00;
+byte Memory[4096]; // TO DO. Add expansion capability to memory.
+
+// FLAGS
+const byte flag_N = 0x80; // Negative (Note that byte cast is necessary here only)
+const byte flag_V = 0x40; // Overflow
+const byte flag_B = 0x10; // BRK command
+const byte flag_D = 0x08; // Decimal mode
+const byte flag_I = 0x04; // IRQ disable
+const byte flag_Z = 0x02; // Zero
+const byte flag_C = 0x01; // Carry
+
+byte getFlag(byte flag) {
+ return ((P & flag) == flag) ? 1 : 0;
+}
+
+void setFlag(byte flag, int x) { //OVERLOAD TO ACCEPT INT AS WELL
+ 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 toggleFlag(byte flag) {
+ P = ((P & flag) == flag) ? (P + flag) : (P - flag);
+}
+
+// BCD
+
+byte toBCD(byte x){
+ if (x < 100){
+ byte a = ((x / 10) << 4);
+ byte b = (x % 10);
+ return (a + b);
+ }
+ else{
+ fprintf(stderr, "Number greater than 99 passed to toBCD()");
+ }
+}
+
+byte fromBCD(byte x){
+ byte a = ((x >> 4) * 10);
+ byte b = (x & 0xF);
+ return (a + b);
+}
+
+
+
+
+
+// Class of instructions
+//each one has every type of addressing mode, inherited from a base class.
+// the different addressing modes will be distinguished when the bytecode is called, it will run the instructions function with a selection for the
+/* example
+
+Immediate ADC #$44 $69 2 2
+instruction(0x69); -> ADC({enum for immediate});
+
+Zero Page ADC $44 $65 2 3
+instruction(0x65); -> ADC({enum for Zero Page});
+*/
+// Set particular flags
+void setFlagN(){
+ if (acc ^ 0x80){
+ setFlag(flag_N, 1);
+ }else{ //not sure if this should be present, I think it is
+ setFlag(flag_N, 0);
+ }
+}
+
+//Perform prior to any changes
+void setFlagV(byte x, byte y){ //This is pathetic.
+ if ((x & flag_N) == (y & flag_N)){
+ if (((x + y) & (flag_N ^ 0xFF)) > 0x8F) setFlag(flag_V, 1);
+ }else{
+ if (((x - y) & (flag_N ^ 0xFF)) > 0x8F) setFlag(flag_V, 1);
+ }
+}
+/*void setFlagB(){ //WORK ON
+ setFlag(flag_B, 1);
+}*/ //Dont really need since its dependent on the BRK insturction
+/*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);
+ }
+}
+void setFlagC(){ // NOTE. Must make setFlagC functional with independence. Look into carrying on 6502
+ setFlag(flag_Z, 1);
+}
+
+
+
+
+
+
+