summaryrefslogtreecommitdiff
path: root/src/cpu/addressing.c
diff options
context:
space:
mode:
authoralekseiplusplus <alekseijeaves@protonmail.com>2023-12-08 06:52:14 +1100
committeralekseiplusplus <alekseijeaves@protonmail.com>2023-12-08 06:52:14 +1100
commitd28ad56810b43cb3ea5d11e5e32042f69ee04562 (patch)
tree0d0fd8976b253bf6aafed6e1449a1d4e69f658ab /src/cpu/addressing.c
parent5012497d6e59d787195d177d02faa55242bf7e40 (diff)
major refactor of cpu code
Diffstat (limited to 'src/cpu/addressing.c')
-rw-r--r--src/cpu/addressing.c161
1 files changed, 27 insertions, 134 deletions
diff --git a/src/cpu/addressing.c b/src/cpu/addressing.c
index b8f9330..8c6c619 100644
--- a/src/cpu/addressing.c
+++ b/src/cpu/addressing.c
@@ -1,15 +1,12 @@
// addressing.h
-// Contains definitions relevant to addressing, as well as fAddress() which returns time, length, value, and address for an instruction function call.
-// Would like to refactor the code into something better, such as switch-case statements for the cycles calculation.
-#include"addressing.h"
+#include "addressing.h"
-//Holds address of current instruction. I don't think it's being used at all besides fAddressGetCycles(), so will be removed.
-void* current_instruction;
-
-address fAddressGetAddress(Addressing mode, address x) {
- switch(mode){
+address fAddressGetAddress(Addressing mode, address x)
+{
+ switch(mode)
+ {
case eImplied:
case eRelative:
case eImmediate:
@@ -18,7 +15,7 @@ address fAddressGetAddress(Addressing mode, address x) {
case eAbsolute:
return x;
case eIndirectAbsolute:
- return (address)GetMemory(x) + (((address)GetMemory(x+1)) << 8);
+ return (((address)GetMemory(x+1)) << 8) + (address)GetMemory(x);
case eAbsoluteIndexedX:
return x + X;
case eAbsoluteIndexedY:
@@ -30,26 +27,31 @@ address fAddressGetAddress(Addressing mode, address x) {
case eZeroPageIndexedY:
return ((x + Y) & 0x00FF);
case eIndexedIndirect:
- return (((address)(GetMemory(x+X+1)))<<8) + (address)(GetMemory(x+X));
+ return (((address)(GetMemory(x+X+1))) << 8) + (address)(GetMemory(x+X));
case eIndirectIndexed:
- return ((address)(GetMemory(x+1))<<8) + ((address)(GetMemory(x))) + ((address)Y);
- //return ((x >> 8) & 0xFF) (x & 0xFF)
- }
+ return ((address)(GetMemory(x+1)) << 8) + ((address)(GetMemory(x))) + ((address)Y);
+ }
}
-int fAddressGetLength(Addressing mode){
- switch(mode){
+
+int fAddressGetLength(Addressing mode)
+{
+ switch(mode)
+ {
case eAbsolute: case eAbsoluteIndexedX: case eAbsoluteIndexedY: case eIndirectAbsolute:
return 3;
case eAccumulator: case eImplied:
return 1;
default:
return 2;
- }
+ }
}
-byte fAddressGetValue(Addressing mode, address x, address addr) {
- switch(mode){
+
+byte fAddressGetValue(Addressing mode, address x, address addr)
+{
+ switch(mode)
+ {
case eImplied:
return 0;
case eRelative:
@@ -59,124 +61,15 @@ byte fAddressGetValue(Addressing mode, address x, address addr) {
return acc;
default:
return GetMemory(addr);
- }
-}
-
-// At the moment not run, and will probably be replaced by just direct storage of values.
-// The code making up this is likely way less performant and larger than just looking up stored values, so I will change it in the future to do that instead.
-int fAddressGetCycles(Addressing mode, address x, address addr) {
- int cycles;
-
- //case &fADC: case &fAND: case &fBIT: case &fCMP: case &fCPX: case &fCPY: case &fEOR: case &fLDA:
- //case &fLDX: case &fLDY: case &fORA: case &fSBC: case &fSTX: case &fSTY:
-
- if ( current_instruction == &fADC || current_instruction == &fAND || current_instruction == &fBIT || current_instruction == &fCMP || current_instruction == &fCPX
- || current_instruction == &fCPY || current_instruction == &fEOR || current_instruction == &fLDA || current_instruction == &fLDX || current_instruction == &fLDY
- || current_instruction == &fORA || current_instruction == &fSBC || current_instruction == &fSTX || current_instruction == &fSTY ){
- switch(mode){
- case eImmediate:
- cycles = 2; break;
- case eZeroPage:
- cycles = 3; break;
- case eZeroPageIndexedX: case eAbsolute: case eAbsoluteIndexedX: case eAbsoluteIndexedY:
- cycles = 4; break;
- case eIndexedIndirect:
- cycles = 6; break;
- case eIndirectIndexed:
- cycles = 5; break;
- }
- }
-
- //case &fASL: case &fDEC: case &fINC: case &fLSR: case &fROL: case &fROR:
- else if( current_instruction == &fASL || current_instruction == &fDEC || current_instruction == &fINC
- || current_instruction == &fLSR || current_instruction == &fROL || current_instruction == &fROR ){
- switch(mode){
- case eAccumulator:
- cycles = 2; break;
- case eZeroPage:
- cycles = 5; break;
- case eZeroPageIndexedX: case eAbsolute:
- cycles = 6; break;
- case eAbsoluteIndexedX:
- cycles = 7; break;
}
- }
-
- //case &fSTA:
- else if (current_instruction == &fSTA){
- switch(mode){
- case eZeroPage:
- cycles = 3; break;
- case eZeroPageIndexedX: case eAbsolute:
- cycles = 4; break;
- case eAbsoluteIndexedX: case eAbsoluteIndexedY:
- cycles = 5; break;
- case eIndexedIndirect: case eIndirectIndexed:
- cycles = 6; break;
- }
- }
-
-
- //case &fBRK:
- else if (current_instruction == &fBRK){
- cycles = 7;
- }
-
-
- //case &fRTI: case &fRTS: case &fJSR:
- else if (current_instruction == &fRTI || current_instruction == &fRTS || current_instruction == &fJSR){
- cycles = 6;
- }
-
- //case &fJMP:
- else if (current_instruction == &fJMP){
- cycles = 5;
- }
-
- //case &fPLA: case &fPLP:
- else if (current_instruction == &fPLA || current_instruction == &fPLP){
- cycles = 4;
- }
-
- //case &fPHA: case &fPHP:
- else if (current_instruction == &fPHA || current_instruction == &fPHP){
- cycles = 3;
- }
-
- else {
- cycles = 2;
- }
-
-
- // Page Boundary
-
- //case &fADC: case &fSBC: case &fLDA: case &fLDX: case &fLDY: case &fEOR: case &fAND: case &fORA: case &fCMP:
- if ( current_instruction == &fADC || current_instruction == &fSBC || current_instruction == &fLDA || current_instruction == &fLDX || current_instruction == &fLDY
- || current_instruction == &fEOR || current_instruction == &fAND || current_instruction == &fORA || current_instruction == &fCMP ){
- switch(mode){
- case eAbsoluteIndexedX:
- if ((x & 0xFF00) != ((x + X) & 0xFF00))
- cycles++;
- break;
- case eAbsoluteIndexedY:
- if ((x & 0xFF00) != ((x + Y) & 0xFF00))
- cycles++;
- break;
- case eIndirectIndexed:
- if ((addr & 0xFF00) != (addr - Y & 0xFF00))
- cycles++;
- break;
- }
- }
-
- return cycles;
}
-AddData fAddress(Addressing mode, address x) {
- AddData ret;
- ret.add = fAddressGetAddress (mode, x);
- ret.value = fAddressGetValue (mode, x, ret.add);
- ret.length = fAddressGetLength (mode);
- //ret.cycles = fAddressGetCycles (mode, x, ret.add);
+
+struct State fAddress(Addressing mode, address x)
+{
+ struct State ret;
+ ret.address = fAddressGetAddress (mode, x);
+ ret.value = fAddressGetValue (mode, x, ret.address);
+ ret.length = fAddressGetLength (mode);
return ret;
}