-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mediator.ts
155 lines (129 loc) · 3.45 KB
/
Mediator.ts
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
interface Bus<Messages> {
notify(message: Messages): void;
}
export class BusConnector<TMessages> {
private bus: Bus<TMessages> | undefined;
connect(bus: Bus<TMessages>) {
this.bus = bus;
}
signal(message: TMessages) {
if (!this.bus) {
throw new Error("bus disconnected");
}
this.bus.notify(message);
}
}
type ReadMessage = {
type: "memory-read";
address: number;
response: (value: number) => void;
};
type WriteMessage = {
type: "memory-write";
value: number;
address: number;
response: (wasOk: boolean) => void;
};
type CPUMemoryMessages = ReadMessage | WriteMessage;
class DataBus implements Bus<CPUMemoryMessages> {
private cpu: CPU;
private memory: Memory;
constructor(cpu: CPU, memory: Memory) {
this.cpu = cpu;
this.memory = memory;
// setup the connections
this.cpu.connect(this);
this.memory.connect(this);
}
notify(message: CPUMemoryMessages): void {
switch (message.type) {
case "memory-read": {
const { address, response } = message;
const value = this.memory.read(address);
response(value);
break;
}
case "memory-write": {
const { address, value, response } = message;
try {
this.memory.write(value, address);
response(true); // writing operation success
} catch (e) {
response(false); // there was an error
}
break;
}
default: {
throw new Error("Invalid message");
}
}
}
}
class Memory extends BusConnector<CPUMemoryMessages> {
private memory: number[];
constructor() {
super();
this.memory = [];
}
read(address: number): number {
console.log(`Memory: reading the address (${address})`);
return this.memory[address];
}
write(value: number, address: number) {
console.log(`Memory: writing the value (${value}) on address (${address})`);
this.memory[address] = value;
}
}
class CPU extends BusConnector<CPUMemoryMessages> {
private nextInstructionAddress: number = 0;
private result: number = 0;
fetch() {
console.log(
`CPU: Triying to fetch an instruction from address ${this.nextInstructionAddress}`
);
let instruction: number;
this.signal({
type: "memory-read",
address: this.nextInstructionAddress,
response: (value) => (instruction = value),
});
console.log(
`CPU: instruction fetched from address ${this.nextInstructionAddress}`
);
// - Increment the nextInstructionAddress
this.nextInstructionAddress++;
}
decode(instruction: number) {
// Not implemented for sake of simplicity
}
execute(opCode: number, operand1: number, operand2: number) {
// run the code associated with the decoded instruction
switch (opCode) {
case 0:
this.result = operand1 + operand2;
break;
case 1:
this.result = operand1 / operand2;
break;
case 2:
console.log(`CPU: Triying write a value on address ${operand2}`);
this.signal({
type: "memory-write",
value: operand1,
address: operand2,
response: (ok) => console.log(`CPU: write operation success: ${ok}`),
});
break;
// ... rest of operations
default: {
throw new Error("Invalid operation");
}
}
}
}
const cpu = new CPU();
const memory = new Memory();
const bus = new DataBus(cpu, memory);
cpu.fetch();
cpu.fetch();
cpu.execute(2, 4, 10);