Skip to content

Commit ba1c383

Browse files
committed
separated risc lib and 'sub' fix
1 parent 847b6e5 commit ba1c383

File tree

8 files changed

+1130
-1078
lines changed

8 files changed

+1130
-1078
lines changed

include/math.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <iostream>
44

55
#include "utils.hpp"
6+
#include "risc.hpp"
67
struct StatusRegister;
78

89
using namespace std;
@@ -133,4 +134,10 @@ namespace math {
133134
* @return The hex string
134135
*/
135136
string StatusRegisterToHexstr(StatusRegister sr);
137+
/**
138+
* @brief Function to calculate the twos complement of a 16bit number
139+
* @param n The number
140+
* @returns The twos complement
141+
*/
142+
Uint16 twosComplement(Uint16 n);
136143
}

include/risc.hpp

Lines changed: 370 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,370 @@
1+
#pragma once
2+
3+
#include "utils.hpp"
4+
struct Logger;
5+
6+
using namespace std;
7+
8+
#define READ true
9+
#define WRITE false
10+
#define MEMORY true
11+
#define INPUTOUTPUT false
12+
#define WORD true
13+
#define BYTE false
14+
15+
struct ControlBus;
16+
struct Instruction;
17+
struct StatusRegister;
18+
19+
/**
20+
* @brief Structure that contains the control bus variables
21+
* @param R Read
22+
* @param M Memory
23+
* @param W Word
24+
*/
25+
struct ControlBus {
26+
bool R, M, W;
27+
/**
28+
* @brief Constructor
29+
*/
30+
ControlBus();
31+
/**
32+
* @brief Constructor
33+
*/
34+
ControlBus(bool r, bool m, bool w);
35+
/**
36+
* @brief Function to reset control bus
37+
*/
38+
void reset();
39+
};
40+
41+
/**
42+
* @brief Structure that contains instruction data
43+
* @param group Instruction group
44+
* @param addressing Addressing method
45+
* @param opcode Operation code
46+
* @param r1 First register
47+
* @param r2 Second register
48+
* @param offset Offset for jmp instructions
49+
*/
50+
struct Instruction {
51+
Uint8 group;
52+
Uint8 addressing;
53+
Uint8 opcode;
54+
Uint8 ra, rb;
55+
Uint8 offset;
56+
/**
57+
* @brief Constructor
58+
*/
59+
Instruction();
60+
};
61+
62+
/**
63+
* @brief Structure that contains the status register variables
64+
* @param Z Zero
65+
* @param C Carry
66+
* @param N Negative
67+
* @param V Overflow
68+
*/
69+
struct StatusRegister {
70+
/**
71+
* @brief Constructor
72+
*/
73+
StatusRegister();
74+
bool Z, N, C, V;
75+
};
76+
77+
class SystemBus;
78+
class ArithmeticLogicUnit;
79+
class CentralProcessingUnit;
80+
class CentralMemory;
81+
class InputOutputDevices;
82+
83+
class SystemBus {
84+
public:
85+
/**
86+
* @brief Constructor
87+
*/
88+
SystemBus();
89+
/**
90+
* @brief Function to write on address bus
91+
* @param a Address to write
92+
*/
93+
void writeAddress(Uint16 a);
94+
/**
95+
* @brief Function to write on data bus
96+
* @param d Data to write
97+
*/
98+
void writeData(Uint16 d);
99+
/**
100+
* @brief Function to write on control bus
101+
* @param c Control to write
102+
*/
103+
void writeControl(ControlBus c);
104+
/**
105+
* @brief Function to read from address bus and empty it
106+
* @return What is written on address bus
107+
*/
108+
Uint16 readAddress();
109+
/**
110+
* @brief Function to read from data bus and empty it
111+
* @return What is written on data bus
112+
*/
113+
Uint16 readData();
114+
/**
115+
* @brief Function to read from control bus and empty it
116+
* @return What is written on control bus
117+
*/
118+
ControlBus readControl();
119+
/**
120+
* @brief Function to get the content of the address bus
121+
* @return What is written on address bus
122+
*/
123+
Uint16 getAddress();
124+
/**
125+
* @brief Function to get the content of the data bus
126+
* @return What is written on data bus
127+
*/
128+
Uint16 getData();
129+
/**
130+
* @brief Function to get the content of the control bus
131+
* @return What is written on control bus
132+
*/
133+
ControlBus getControl();
134+
/**
135+
* @brief Function to reset the system bus
136+
*/
137+
void resetBus();
138+
private:
139+
Uint16 AB; //Address Bus
140+
Uint16 DB; //Data Bus
141+
ControlBus CB; //Control Bus
142+
};
143+
144+
class ArithmeticLogicUnit {
145+
public:
146+
/**
147+
* @brief Constructor
148+
*/
149+
ArithmeticLogicUnit(StatusRegister* pSR);
150+
/**
151+
* @brief Function to add two numbers, d += s
152+
* @param d Destination register
153+
* @param s Source register
154+
*/
155+
void add(Uint8 d, Uint8 s);
156+
/**
157+
* @brief Function to subtruct two numbers, d -= s
158+
* @param d Destination register
159+
* @param s Source register
160+
*/
161+
void sub(Uint8 d, Uint8 s);
162+
/**
163+
* @brief Function to do bitwise not, r = ~r
164+
* @param r Register
165+
*/
166+
void bNot(Uint8 r);
167+
/**
168+
* @brief Function to do bitwise and, d = d & s
169+
* @param d Destination register
170+
* @param s Source register
171+
*/
172+
void bAnd(Uint8 d, Uint8 s);
173+
/**
174+
* @brief Function to do bitwise or, d = d | s
175+
* @param d Destination register
176+
* @param s Source register
177+
*/
178+
void bOr(Uint8 r, Uint8 s);
179+
/**
180+
* @brief Function to do bitwise xor, d = d ^ s
181+
* @param d Destination register
182+
* @param s Source register
183+
*/
184+
void bXor(Uint8 d, Uint8 s);
185+
/**
186+
* @brief Function to increment, r++
187+
* @param r Register
188+
*/
189+
void inc(Uint8 r);
190+
/**
191+
* @brief Function to decrement, r--
192+
* @param r Register
193+
*/
194+
void dec(Uint8 r);
195+
/**
196+
* @brief Function to left shift, r << 1
197+
* @param r Register
198+
*/
199+
void lShift(Uint8 r);
200+
/**
201+
* @brief Function to right shift, r >> 1
202+
* @param r Register
203+
*/
204+
void rShift(Uint8 r);
205+
/**
206+
* @brief Function to load a word in a register
207+
* @param r Register number
208+
* @param word The word to load
209+
*/
210+
void load(Uint8 r, Uint16 word);
211+
/**
212+
* @brief Funtion to get the word from a register
213+
* @param r The register number
214+
*/
215+
Uint16 get(Uint8 r);
216+
private:
217+
Uint16 R[16]; //Registers from 0 to 15
218+
StatusRegister* SR; //Status register pointer
219+
};
220+
221+
class CentralProcessingUnit{
222+
public:
223+
/**
224+
* @brief Constructor
225+
* @param pSB System bus pointer
226+
* @param pCM Central memory pointer
227+
* @param pIOD Input/output devices pointer
228+
* @param start Address of first program code line
229+
*/
230+
CentralProcessingUnit(SystemBus* pSB, CentralMemory* pCM, InputOutputDevices* pIOD, Uint16 start);
231+
/**
232+
* @brief Function that fetches the instruction from the memory
233+
*/
234+
void fetchInstruction();
235+
/**
236+
* @brief Function to decodes the instruction
237+
*/
238+
void decodeInstruction();
239+
/**
240+
* @brief Function to fetch the operand
241+
*/
242+
void fetchOperand();
243+
/**
244+
* @brief Function to execute the instruction
245+
*/
246+
void executeInstruction();
247+
/**
248+
* @brief Function to get the Program Counter value
249+
* @returns The program counter value
250+
*/
251+
Uint16 getPC();
252+
/**
253+
* @brief Function to get the Instruction Register value
254+
* @returns The instruction register value
255+
*/
256+
Uint16 getIR();
257+
/**
258+
* @brief Function to get the Status Register value
259+
* @returns The status register value
260+
*/
261+
StatusRegister getSR();
262+
/**
263+
* @brief Function to get the Address Register value
264+
* @returns The adress register value
265+
*/
266+
Uint16 getAR();
267+
/**
268+
* @brief Function to get the Data Register value
269+
* @returns The data register value
270+
*/
271+
Uint16 getDR();
272+
/**
273+
* @brief Function to get the Stack Pointer value
274+
* @returns The stack pointer value
275+
*/
276+
Uint16 getSP();
277+
/**
278+
* @brief Function to get the instruction name
279+
* @return The instriction name, type string
280+
*/
281+
string getInstName();
282+
/**
283+
* @brief Function to get a register from the ALU that is private
284+
* @param r The registrer number
285+
* @returns The value, type Uint16
286+
*/
287+
Uint16 getR(Uint8 r);
288+
/**
289+
* @brief Function to get the two phases
290+
* @param now Variable in which store phase now
291+
* @param next Variable in which store phase next
292+
*/
293+
void getPhases(Uint8 &now, Uint8 &next);
294+
private:
295+
Uint16 PC; //Program Counter
296+
Uint16 SP; //Stack Pointer
297+
Uint16 IR; //Instruction Register
298+
Uint16 AR; //Address Register
299+
Uint16 DR; //Data Register
300+
StatusRegister SR; //Status Register
301+
Instruction I; //Decoded Instruction
302+
ArithmeticLogicUnit ALU; //Arithmetic Logic Unit
303+
SystemBus* SB; //System Bus pointer
304+
CentralMemory* CM; //Central Memory pointer
305+
InputOutputDevices* IOD; //Input Output Devices pointer
306+
Uint8 phaseNow, phaseNext; //Phases 0:IF 1:ID 2:OF 3:IE
307+
string instName; //Instruction name, for GUI
308+
/**
309+
* @brief Function to decode the instruction name
310+
* @returns The instruction name
311+
*/
312+
string decodeInstName();
313+
/**
314+
* @brief Function that loads a word, that is after the instruction, in a register
315+
*/
316+
void ldwi();
317+
/**
318+
* @brief Function that loads a word, from an address that is after the instruction, in a register
319+
*/
320+
void ldwa();
321+
/**
322+
* @brief Function that loads a word, from an address that is in a register, in a register
323+
*/
324+
void ldwr();
325+
/**
326+
* @brief Function that loads a byte, that is after the instruction, in a register
327+
*/
328+
void ldbi();
329+
/**
330+
* @brief Function that loads a byte, from an address that is after the instruction, in a register
331+
*/
332+
void ldba();
333+
/**
334+
* @brief Function that loads a byte, from an address that is in a register, in a register
335+
*/
336+
void ldbr();
337+
};
338+
339+
class CentralMemory {
340+
public:
341+
/**
342+
* @brief Contructor
343+
* @param pSB System bus pointer
344+
* @param psize Memory fixed size
345+
*/
346+
CentralMemory(SystemBus* pSB, Uint16 psize);
347+
/**
348+
* @brief Program to load the program
349+
*/
350+
void loadProgram(string path, bool bin, Logger* logger);
351+
/**
352+
* @brief Function that reads the system bus and operate
353+
*/
354+
void operate();
355+
private:
356+
vector<Uint8> M; //All memory bytes
357+
Uint16 size; //Memory size
358+
SystemBus* SB; //System Bus pointer
359+
};
360+
361+
class InputOutputDevices {
362+
public:
363+
/**
364+
* @brief Constructor
365+
* @param pSB System bus pointer
366+
*/
367+
InputOutputDevices(SystemBus* pSB);
368+
private:
369+
SystemBus* SB; //System Bus pointer
370+
};

0 commit comments

Comments
 (0)