/
machine.hpp
132 lines (107 loc) · 2.56 KB
/
machine.hpp
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
/*
* machine.hpp
*
* Baker, Ballard, Jager-Kujawa
* CSC 341
* Spring 2016
*/
#ifndef MACHINE
#define MACHINE
#include <string> // string
#include <iostream> // cout, cerr
#include <iomanip> // boolalpha, setw, setfill, dec, hex, endl
#include <unistd.h> // usleep
#include "utils.hpp"
using namespace std;
#define normal "\033[0m"
#define red "\033[31m"
#define green "\033[32m"
#define blue "\033[34m"
// Debugging flags
#define DEBUG
#define DEBUG_VERBOSE
#undef DEBUG_VERBOSE
// #undef DEBUG
// If DEBUG_VERBOSE, then DEBUG
#ifdef DEBUG_VERBOSE
#ifndef DEBUG
#define DEBUG
#endif
#endif
/* Define condition codes for hardware functions
> : 001
< : 100
= : 010
!= : 101
>= : 011
<= : 110
*/
#define GRT 1
#define LST 4
#define EQL 2
#define NEQ 5
#define GEQ 3
#define LEQ 6
// Define addressing modes
#define DIRECT 0
#define IMMEDIATE 1
#define QUANTUM 3
// Define machine registers type
// Machine register type also has a condition code register
struct registers{
unsigned short int r1, r2, r3, rA, IR, PC:8;
unsigned short int CR:3;
PageTable* PTBR;
registers(int pR1, int pR2, int pR3, int pRA, int pIR, int pPC, int pCR) :
r1(pR1), r2(pR2), r3(pR3), rA(pRA), IR(pIR), PC(pPC), CR(pCR), PTBR(NULL) {}
};
extern int *sysclock;
extern int timer_interrupt;
// Declare machine registers
extern registers machine;
// Declare main memory
extern unsigned short int main_memory[256];
// Memory Management Unit
// Takes a logical address and converts it to a physical address
// by looking up frame numbers in the table store in PTBR
unsigned short int MMU(unsigned short int logicalAddress);
// Uses value in IR to determine course of action
// Returns false if errors
bool interpreter();
// Retrieves the opcode
// Returns an int 0-15
unsigned short int getOpcode(unsigned short int num);
// Retrieves the address mode
// Returns 0 or 1
unsigned short int getAddrMode(unsigned short int num);
// Retrieves the register code
// Returns an int 0-3
unsigned short int getRegCode(unsigned short int num);
// Retrieves the operand
// Returns an int 0-255
unsigned short int getOperand(unsigned short int num);
// Opcode functions
bool LOD();
bool STO();
bool ADD();
bool SUB();
bool ADR();
bool SUR();
bool AND();
bool IOR();
bool NOT();
bool JMP();
bool JEQ();
bool JGT();
bool JLT();
bool CMP();
bool CLR();
bool HLT();
// Determines if a result is >, <, = (or +, -, 0)
unsigned short int getCondCode(unsigned short int);
// Take a register code and return a pointer to that register
unsigned short int* getRegister();
#ifdef DEBUG
void printDebug(string);
#endif
#endif