-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPU.cs
142 lines (125 loc) · 4.49 KB
/
CPU.cs
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
// Filename: CPU.cs
// Author: Taylor Eernisse
// Date: 9/18/12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace armsimGUI
{
/// <summary>
/// Class: CPU
/// Purpose: Handles the main CPU logic.
/// Methods: Fetch()
/// Decode()
/// Execute()
/// TestCPU()
/// </summary>
class CPU
{
public Memory progRAM { get; private set; } // Memory object containing main program memory
public Memory registers { get; private set; } // Memory object containing register memory
public int stepNum { get; private set; } // contains current step counter value
public uint pcCopy { get; set; } // copy of program counter for proper trace output
public bool NFlag { get; private set; } // N flag
public bool ZFlag { get; private set; } // Z flag
public bool CFlag { get; private set; } // C flag
public bool FFlag { get; private set; } // F flag
public bool IFlag { get; private set; } // I flag
//--------------------------------------------------------------
// Purpose: CPU constructor
//--------------------------------------------------------------
public CPU(Memory _ram, Memory _registers)
{
progRAM = _ram;
registers = _registers;
stepNum = 0;
NFlag = false;
ZFlag = false;
CFlag = false;
FFlag = false;
IFlag = false;
}
//--------------------------------------------------------------
// Purpose: references data stored in memory at the current Program Counter address
// Returns: the value in the address stored in the Program Counter
//--------------------------------------------------------------
public uint Fetch()
{
uint val = registers.ReadWord((uint)regs.PC);
uint data = progRAM.ReadWord(val);
// increment program counter
registers.WriteWord((uint)regs.PC, val + 4);
return data;
}
//--------------------------------------------------------------
// Purpose: Processes <data>, creating an Instruction object with appropriate
// property values filled
// Returns: An Instruction representing <data>
//--------------------------------------------------------------
public Instruction Decode(uint data)
{
InstructionFactory IF = new InstructionFactory();
Instruction i = IF.Create(data);
return i;
}
//--------------------------------------------------------------
// Purpose: Executes <instr>
// Returns: nothing
//--------------------------------------------------------------
public void Execute(Instruction instr, bool generateDisasm)
{
instr.ExecuteInstruction(false);
stepNum += 1;
}
//--------------------------------------------------------------
// Purpose: Performs an addition according to data in <instr>
// Returns: nothing
//--------------------------------------------------------------
public static void TestCPU()
{
Trace.WriteLine("Testing CPU...");
Memory ram = new Memory(32768);
Memory reg = new Memory(64);
reg.WriteWord((uint)regs.PC, 2300);
ram.WriteWord((uint)2300, 0x12345678);
CPU cpu = new CPU(ram, reg);
uint data = cpu.Fetch();
Debug.Assert(data == 0x12345678);
Trace.WriteLine("Fetch() test passed!");
// TODO: cpu.Execute();
Debug.Assert(reg.ReadWord((uint)regs.PC) == 2304);
Trace.WriteLine("Execute() test passed!");
}
public void SetNFlag(bool b)
{
NFlag = b;
}
public void SetZFlag(bool b)
{
ZFlag = b;
}
public void SetCFlag(bool b)
{
CFlag = b;
}
public void SetFFlag(bool b)
{
FFlag = b;
}
public void SetIFlag(bool b)
{
IFlag = b;
}
public void ClearFlags()
{
NFlag = false;
ZFlag = false;
CFlag = false;
FFlag = false;
}
}
}