Skip to content

Commit 847b6e5

Browse files
committed
phases
1 parent 1d1176a commit 847b6e5

File tree

5 files changed

+97
-28
lines changed

5 files changed

+97
-28
lines changed

include/math.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ struct StatusRegister;
77

88
using namespace std;
99

10+
typedef int8_t Int8;
11+
typedef int16_t Int16;
12+
typedef int32_t Int32;
13+
ostream& operator << (ostream& os, Uint8 n);
14+
ostream& operator << (ostream& os, Int8 n);
15+
1016
struct Vector2f;
1117
struct Vector2d;
1218
struct HitBox2d;

include/utils.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
using namespace std;
2121
using namespace Json;
2222

23-
typedef int8_t Int8;
24-
typedef int16_t Int16;
25-
typedef int32_t Int32;
26-
2723
struct ControlBus;
2824
struct Instruction;
2925
struct StatusRegister;
@@ -378,6 +374,9 @@ class CentralProcessingUnit{
378374
* @brief Function to fetch the operand
379375
*/
380376
void fetchOperand();
377+
/**
378+
* @brief Function to execute the instruction
379+
*/
381380
void executeInstruction();
382381
/**
383382
* @brief Function to get the Program Counter value
@@ -414,6 +413,18 @@ class CentralProcessingUnit{
414413
* @return The instriction name, type string
415414
*/
416415
string getInstName();
416+
/**
417+
* @brief Function to get a register from the ALU that is private
418+
* @param r The registrer number
419+
* @returns The value, type Uint16
420+
*/
421+
Uint16 getR(Uint8 r);
422+
/**
423+
* @brief Function to get the two phases
424+
* @param now Variable in which store phase now
425+
* @param next Variable in which store phase next
426+
*/
427+
void getPhases(Uint8 &now, Uint8 &next);
417428
private:
418429
Uint16 PC; //Program Counter
419430
Uint16 SP; //Stack Pointer

src/main.cpp

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111

1212
using namespace std;
1313

14-
void randFunc(int a) {
15-
cout << "Here is a button action! " << a << endl;
16-
}
17-
1814
int main(int argc, char* args[]) {
1915
//Variables
2016
Logger logger;
@@ -31,17 +27,15 @@ int main(int argc, char* args[]) {
3127
Uint8 cursorState = 0; //0 -> normal, 1 -> hover, 2 -> normal clicked, 3 -> hover clicked
3228
Cursor cursor = JsonManager::getCursor();
3329
vector<HitBox2d> hitboxes = {HitBox2d(0, 0, 10, 10), HitBox2d(0, 100, 16, 16)}; //Vector of all hitboxes
34-
Uint8 inHitboxes = 0;
35-
bool clicked = false;
30+
Uint8 inHitboxes = 0, phaseNow, phaseNext;
31+
bool clicked = false, refresh = true, constantRefresh = false;
3632

3733
//Interpreter
3834
SystemBus SB;
3935
CentralMemory CM(&SB, settings.interpreter.ramSize);
4036
InputOutputDevices IOD(&SB);
4137
CentralProcessingUnit CPU(&SB, &CM, &IOD, settings.interpreter.start);
4238
CM.loadProgram(settings.interpreter.file, settings.interpreter.binary, &logger);
43-
CPU.fetchInstruction();
44-
CPU.decodeInstruction();
4539

4640
//Capturing cout in log file
4741
if(settings.console.log) freopen("log.txt", "w", stdout);
@@ -205,27 +199,52 @@ int main(int argc, char* args[]) {
205199
if(cursorState >= 2) fastButton.changePressed();
206200
else fastButton.changeNormal();
207201
inHitboxes++;
202+
if(clicked) constantRefresh = true;
208203
}
209204
if(guiCursorPosition == *playButton.getHitBox()) {
210205
if(cursorState >= 2) playButton.changePressed();
211206
else playButton.changeNormal();
212207
inHitboxes++;
213-
if(clicked) playButton.action(randFunc);
208+
if(clicked) refresh = true;
214209
}
215210
if(guiCursorPosition == *nextButton.getHitBox()) {
216211
if(cursorState >= 2) nextButton.changePressed();
217212
else nextButton.changeNormal();
218213
inHitboxes++;
214+
if(clicked) {
215+
refresh = true;
216+
switch(phaseNext) {
217+
case 0:
218+
CPU.fetchInstruction();
219+
break;
220+
case 1:
221+
CPU.decodeInstruction();
222+
break;
223+
case 2:
224+
CPU.fetchOperand();
225+
break;
226+
case 3:
227+
CPU.executeInstruction();
228+
}
229+
}
219230
}
220231
if(guiCursorPosition == *pauseButton.getHitBox()) {
221232
if(cursorState >= 2) pauseButton.changePressed();
222233
else pauseButton.changeNormal();
223234
inHitboxes++;
235+
if(clicked) {
236+
constantRefresh = false;
237+
refresh = true;
238+
}
224239
}
225240
if(guiCursorPosition == *stopButton.getHitBox()) {
226241
if(cursorState >= 2) stopButton.changePressed();
227242
else stopButton.changeNormal();
228243
inHitboxes++;
244+
if(clicked) {
245+
constantRefresh = false;
246+
refresh = true;
247+
}
229248
}
230249
if(inHitboxes > 0) {
231250
if(cursorState == 0) cursorState = 1;
@@ -243,13 +262,22 @@ int main(int argc, char* args[]) {
243262
}
244263

245264
instNameValue = CPU.getInstName();
246-
//CPU Values
247-
pcValue = "0x" + math::Uint16Tohexstr(CPU.getPC());
248-
irValue = "0x" + math::Uint16Tohexstr(CPU.getIR());
249-
srValue = math::StatusRegisterToHexstr(CPU.getSR());
250-
arValue = "0x" + math::Uint16Tohexstr(CPU.getAR());
251-
drValue = "0x" + math::Uint16Tohexstr(CPU.getDR());
252-
spValue = "0x" + math::Uint16Tohexstr(CPU.getSP());
265+
if(refresh || constantRefresh) {
266+
//CPU Values
267+
pcValue = "0x" + math::Uint16Tohexstr(CPU.getPC());
268+
irValue = "0x" + math::Uint16Tohexstr(CPU.getIR());
269+
srValue = math::StatusRegisterToHexstr(CPU.getSR());
270+
arValue = "0x" + math::Uint16Tohexstr(CPU.getAR());
271+
drValue = "0x" + math::Uint16Tohexstr(CPU.getDR());
272+
spValue = "0x" + math::Uint16Tohexstr(CPU.getSP());
273+
refresh = false;
274+
for(Uint8 i = 0; i < 16; i++) {
275+
registriesValues[i] = "0x" + math::Uint16Tohexstr(CPU.getR(i));
276+
}
277+
CPU.getPhases(phaseNow, phaseNext);
278+
progressBarNowEntity.setX(178 + 8 * phaseNow);
279+
progressBarNextEntity.setX(178 + 8 * phaseNext);
280+
}
253281
//GUI backgrounds
254282
Window.renderGui(cpuGui);
255283
//CPU Render
@@ -283,8 +311,12 @@ int main(int argc, char* args[]) {
283311
Window.renderText(progressBarOfTitle);
284312
Window.renderText(progressBarIeTitle);
285313
Window.renderGui(progressBarEntity);
286-
Window.renderGui(progressBarNowEntity);
287-
Window.renderGui(progressBarNextEntity);
314+
if(phaseNow < 4) {
315+
Window.renderGui(progressBarNowEntity);
316+
}
317+
if(phaseNext < 4) {
318+
Window.renderGui(progressBarNextEntity);
319+
}
288320
//Buttons
289321
Window.renderButton(fastButton);
290322
Window.renderButton(playButton);

src/math.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
using namespace std;
44

5+
ostream& operator << (ostream& os, Uint8 n) {
6+
os << Uint16(n);
7+
return os;
8+
}
9+
10+
ostream& operator << (ostream& os, Int8 n) {
11+
os << Int16(n);
12+
return os;
13+
}
14+
515
Vector2f::Vector2f() :x(0.0), y(0.0) {}
616

717
Vector2f::Vector2f(float px, float py) :x(px), y(py) {}

src/utils.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ void SystemBus::resetBus() {
279279
}
280280

281281
ArithmeticLogicUnit::ArithmeticLogicUnit(StatusRegister* pSR) :SR(pSR) {
282-
for(Uint8 i = 0; i < 0xF; i++) {
282+
for(Uint8 i = 0; i <= 0xF; i++) {
283283
R[i] = 0x0000;
284284
}
285285
}
@@ -396,7 +396,7 @@ Uint16 ArithmeticLogicUnit::get(Uint8 r) {
396396
}
397397

398398
CentralProcessingUnit::CentralProcessingUnit(SystemBus* pSB, CentralMemory* pCM, InputOutputDevices* pIOD, Uint16 start)
399-
:ALU(ArithmeticLogicUnit(&SR)), SB(pSB), CM(pCM), IOD(pIOD), PC(start) {}
399+
:ALU(ArithmeticLogicUnit(&SR)), SB(pSB), CM(pCM), IOD(pIOD), PC(start), phaseNow(0xFF), phaseNext(0x0), instName("-----") {}
400400

401401
void CentralProcessingUnit::fetchInstruction() {
402402
AR = PC;
@@ -640,6 +640,16 @@ string CentralProcessingUnit::getInstName() {
640640
return instName;
641641
}
642642

643+
Uint16 CentralProcessingUnit::getR(Uint8 r) {
644+
return ALU.get(r);
645+
}
646+
647+
void CentralProcessingUnit::getPhases(Uint8 &now, Uint8 &next) {
648+
now = phaseNow;
649+
next = phaseNext;
650+
return;
651+
}
652+
643653
string CentralProcessingUnit::decodeInstName() {
644654
switch(I.group) {
645655
case 0x0: //Data transfer group
@@ -849,16 +859,16 @@ void CentralMemory::loadProgram(string path, bool bin, Logger* logger) {
849859
return;
850860
}
851861
if(bin) {
852-
char s[9];
862+
char s[100];
853863
for(Uint16 i = 0; i < size && !file.eof(); i++) {
854-
file.getline(s, 9);
864+
file.getline(s, 100);
855865
M[i] = math::binstrToUint8(s);
856866
}
857867
}
858868
else {
859-
char s[3];
869+
char s[100];
860870
for(Uint16 i = 0; i < size && !file.eof(); i++) {
861-
file.getline(s, 3);
871+
file.getline(s, 100);
862872
M[i] = math::hexstrToUint8(s);
863873
}
864874
}

0 commit comments

Comments
 (0)