summaryrefslogtreecommitdiff
path: root/main.cc
blob: 3df70d842756895a5088cc083881cc5d71c25e30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import <cstdio>;
import <cstdint>;
import <cstdlib>;

using Byte = unsigned char;
using Word = uint16_t;

struct Mem {
  static constexpr uint32_t MAX_MEM = 1024 * 64;
  Byte Data[MAX_MEM];

  auto Initialise() -> void {
    for (uint32_t i{}; i < MAX_MEM; ++i) {
      Data[i] = 0;
    }
  }

  // Read 1 byte
  auto operator[](uint32_t Address) const -> Byte { return Data[Address]; }

  auto operator[](uint32_t Address) -> Byte { return Data[Address]; }

  // write 2 bytes
  auto WriteWord(Word Value, uint32_t Address, uint32_t &Cycles) -> void {
    Data[Address] = Value & 0xFF;
    Data[Address + 1] = (Value >> 8);
    Cycles -= 2;
  }
};

struct CPU {

  Word PC{}; // Program counter
  Word SP{}; // Stack pointer

  Byte A{}, X{}, Y{}; // registers

  // Status flags
  Byte C : 1;
  Byte Z : 1;
  Byte I : 1;
  Byte D : 1;
  Byte B : 1;
  Byte V : 1;
  Byte N : 1;

  auto Reset(Mem &mem) -> void {
    PC = 0xFFFC;
    SP = 0x0100;
    C = 0;
    Z = 0;
    I = 0;
    D = 0;
    B = 0;
    V = 0;
    N = 0;
    A = 0;
    X = 0;
    Y = 0;

    mem.Initialise();
  }

  auto FetchByte(uint32_t &Cycles, Mem &mem) -> Byte {
    Byte Data{mem[PC]};
    ++PC;
    --Cycles;
    return Data;
  }

  auto FetchWord(uint32_t &Cycles, Mem &mem) -> Word {
    // 6502 is little endian
    Word Data{mem[PC]};
    ++PC;

    Data |= (mem[PC] << 8);
    ++PC;

    Cycles += 2;

    return Data;
  }

  auto ReadByte(uint32_t &Cycles, Byte Address, Mem &mem) -> Byte {
    Byte Data{mem[Address]};
    --Cycles;
    return Data;
  }

  // opcodes
  static constexpr Byte INS_LDA_IM{0xA9}, INS_LDA_ZP{0xA5}, INS_LDA_ZPX{0xB5},
      INS_JSR{0x20};

  auto LDASetStatus() -> void {
    Z = (A == 0);
    N = (A & 0b10'000'000) > 0;
  }

  auto Execute(uint32_t Cycles, Mem &mem) -> void {
    while (Cycles > 0) {
      Byte Ins{FetchByte(Cycles, mem)};
      switch (Ins) {
      case INS_LDA_IM: {
        Byte Value{FetchByte(Cycles, mem)};
        A = Value;
        LDASetStatus();
        break;
      }
      case INS_LDA_ZP: {
        Byte ZeroPageAddress{FetchByte(Cycles, mem)};
        A = ReadByte(Cycles, ZeroPageAddress, mem);
        LDASetStatus();
        break;
      }
      case INS_LDA_ZPX: {
        Byte ZeroPageAddress{FetchByte(Cycles, mem)};
        ZeroPageAddress += X;
        --Cycles;
        A = ReadByte(Cycles, ZeroPageAddress, mem);
        LDASetStatus();
        break;
      }
      case INS_JSR: {
        Word SubAddr{FetchWord(Cycles, mem)};
        mem[SP] = PC - 1;
        mem.WriteWord(PC - 1, SP, Cycles);
        PC = SubAddr;
        --Cycles;
        break;
      }
      default: {
        printf("Instruction not handled %d", Ins);
      }
      }
    }
  }
};

int main() {
  Mem mem{};
  CPU cpu{};
  cpu.Reset(mem);
  cpu.Execute(2, mem);
  return 0;
}