-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsim.d
155 lines (124 loc) · 5.39 KB
/
msim.d
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
146
147
148
149
150
151
152
153
154
155
/********************************************************************************
* masm.d: main file for mips assembler/disassembler *
* 2016 - Ben Perlin *
*******************************************************************************/
import std.algorithm: sort;
import std.array;
import std.exception;
import std.format;
import std.getopt;
import std.process;
import std.stdio;
import terminal; // from Adam Druppe's github
import mips;
int main(string[] args) {
string startAddress = "0x0040_0000";
string traceFilename;
string inputFilename; /* use stdin if empty */
auto helpInformation = getopt(args,
std.getopt.config.passThrough,
"start|s", &startAddress,
"output-file|o", &traceFilename);
if (helpInformation.helpWanted) {
defaultGetoptPrinter("Usage msim [filename]", helpInformation.options);
return 0;
}
if (args.length == 2) {inputFilename = args[1];}
else if (args.length > 2) {
stderr.writeln("Usage msim [filename]");
return 1;
}
// if using file output for trace file
File traceFile = stdout;
if (traceFilename != "") {
try {
traceFile = File(traceFilename, "w");
} catch (Throwable o) {
stderr.writeln("Failed to open file: ", traceFilename);
return 1;
}
}
auto mips = new MIPS();
try {
assembleAndLoad(mips, inputFilename, startAddress);
} catch (Exception except) {
stderr.writeln("Error in assembler: ", except.msg);
return 1;
}
mips.simulate();
mips.showMemDump();
return 0;
}
void assembleAndLoad(MIPS mips, string inputFile, string startAddress) {
auto assemblerOutput = pipe();
string[] args = ["./masm", "-s", startAddress];
if (inputFile != "") args ~= inputFile; // otherwise use stdin
auto apid = spawnProcess(args, stdin, assemblerOutput.writeEnd);
scope(failure) wait(apid);
uint address, binaryContents, hexContents;
bool started = false;
foreach (line; assemblerOutput.readEnd.byLine) {
auto n = line.formattedRead("[0x%x] %b 0x%x", &address, &binaryContents, &hexContents);
enforce(n == 3, "Expected three terms");
enforce(binaryContents == hexContents, format("address [0x%08X]:"
" binary and hex instructions do not match", address));
enforce(!(address&3), format("address not word aligned, [0x%08X]", address));
if (!started) {
mips.PC = address;
started = true;
}
mips.memory.store[address] = hexContents;
}
mips.exitPoint = address + 4;
enforce (wait(apid) == 0);
}
/// quick and dirty printing utility
/// designed to function like an extension method (using UFCS)
void simulate(MIPS mips) {
auto terminal = Terminal(ConsoleOutputType.cellular);
auto input = RealTimeConsoleInput(&terminal, ConsoleInputFlags.raw);
with (terminal) with (mips)
while (!mips.finished) {
mips.step(); // skip start state
terminal.clear();
writefln("Time: %d\t Control-State: %s", time, control.state);
writefln("PC: %s\tIR: %s\tMDR: %s\tA: %s\tB: %s\tALUout: %s", PC, IR, MDR, A, B, ALUout);
writefln("Opcode: %s,\trs: $%d, \trt: $%d, \trd: $%d, \t shamt %d,\t funct: %s",
cast(Opcode) opcode, rs, rt, rd, shamt, cast(Funct) funct);
writefln("immediate: 0x%08X, address 0x%7X", immediate, address);
writeln("\nControl-Signals:");
writefln(" PCWrite: %b, PCWriteCond: %b, PCWriteCondNEQ: %b, PCsource: %d",
control.PCWrite, control.PCWriteCond, control.PCWriteCondNEQ, control.PCsource);
writefln(" IorD: %b, MemRead: %b, MemWrite %b, MemToReg: %b",
control.IorD, control.MemRead, control.MemWrite, control.MemToReg);
writefln(" RegDest: %d, RegWrite: %b", control.RegDest, control.RegWrite);
writefln(" ALUsrcA: %b, ALUsrcB: %d, ALUmasterOp: %s",
control.ALUsrcA, control.ALUsrcB, control.ALUmasterOp);
writefln("\nALU: aluOp: %s,\talu: 0x%08x, zero: %b", aluControl, alu, zero);
writeln("\nRegister-File:");
foreach (i; 0..8) {
enum fmt = " $%-2d: 0x%08X".replicate(4);
with (regFile) writefln(fmt, i, registers[i], i+8, registers[i+8],
i+16, registers[i+16], i+24, registers[i+24]);
}
writeln("\nMemory Changes: ", memory.dirty ?
format("[0x%08X] 0x%08X", memory.dirtyAddress, memory.dirtyData) : "");
auto ch = input.getch(); // wait for input
if (ch == 'q') break;
}
}
void showMemDump(MIPS mips) {
auto less = pipeProcess(["/usr/bin/less"], Redirect.stdin);
less.stdin.writeln("Register-File:");
foreach (i; 0..8) {
enum fmt = " $%-2d: 0x%08X".replicate(4);
with (mips.regFile) less.stdin.writefln(fmt, i, registers[i], i+8, registers[i+8],
i+16, registers[i+16], i+24, registers[i+24]);
}
less.stdin.writeln("\nMemory Dump\n[ address ]\tcontents");
foreach (pair; mips.memory.store.byKeyValue.array.sort!((a,b)=>(a.key < b.key))) {
less.stdin.writefln("[0x%08X]\t0x%08X", pair.key, pair.value);
}
less.stdin.close();
wait(less.pid);
}