-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
143 lines (138 loc) · 6.18 KB
/
main.c
File metadata and controls
143 lines (138 loc) · 6.18 KB
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
#include <stdio.h>
#include <string.h>
#define AO 0b1000000000000000
#define AI 0b0100000000000000
#define BO 0b0010000000000000
#define BI 0b0001000000000000
#define IO 0b0000100000000000
#define II 0b0000010000000000
#define CO 0b0000001000000000
#define J 0b0000000100000000
#define CE 0b0000000010000000
#define MI 0b0000000001000000
#define RO 0b0000000000100000
#define RI 0b0000000000010000
#define ALUO 0b0000000000001000
#define SUBF 0b0000000000000100
#define NEXT 0b0000000000000010
#define HALT 0b0000000000000001
// EXTRA
#define FI 0b1000000000000000
#define SI 0b0100000000000000
#define RNDO 0b0010000000000000
#define TTYOUT 0b0001000000000000
#define TTYC 0b0000100000000000
#define KBC 0b0000010000000000
#define KBO 0b0000001000000000
// FLAGS
#define IF 0b01000000
#define CF 0b00100000
#define ZF 0b00010000
//INSTRUCTIONS
#define NOP 0b0000
#define LDA 0b0001
#define ADD 0b0010
#define SUB 0b0011
#define JMP 0b0100
#define JZ 0b0101
#define JC 0b0110
#define LDS 0b0111
#define RND 0b1000
#define STORE 0b1001
#define JNI 0b1010
#define READ 0b1011
#define PRINT 0b1100
#define CLS 0b1101
#define NS 0b1110
#define HLT 0b1111
__uint16_t instructions[16 * 8][8] = {
{CO|MI, RO|II|CE, 0, 0, 0, 0, 0, 0}, //0000 - NOP
{CO|MI, RO|II|CE, IO|MI, RO|AI, 0, 0, 0, 0}, //0001 - LDA
{CO|MI, RO|II|CE, IO|MI, RO|BI, ALUO|AI, 0, 0, 0}, //0010 - ADD
{CO|MI, RO|II|CE, IO|MI, RO|BI, ALUO|AI|SUBF, 0, 0, 0}, //0011 - SUB
{CO|MI, RO|II|CE, IO|J, 0, 0, 0, 0, 0}, //0100 - JMP
{CO|MI, RO|II|CE, 0, 0, 0, 0, 0, 0}, //0101 - JZ
{CO|MI, RO|II|CE, 0, 0, 0, 0, 0, 0}, //0110 - JC
{CO|MI, RO|II|CE, IO, 0, 0, 0, 0, 0}, //0111 - LDS
{CO|MI, RO|II|CE, AI, 0, 0, 0, 0, 0}, //1000 - RND
{CO|MI, RO|II|CE, IO|MI, RI|AO, 0, 0, 0, 0}, //1001 - STORE
{CO|MI, RO|II|CE, IO|J, 0, 0, 0, 0, 0}, //1010 - JNI
{CO|MI, RO|II|CE, 0, 0, 0, 0, 0, 0}, //1011 - READ
{CO|MI, RO|II|CE, AO, 0, 0, 0, 0, 0}, //1100 - PRINT
{CO|MI, RO|II|CE, 0, 0, 0, 0, 0, 0}, //1101 - CLS
{CO|MI, RO|II|CE, AO|NEXT, 0, 0, 0, 0, 0}, //1110 - NS
{CO|MI, RO|II|CE, HALT, 0, 0, 0, 0, 0}, //1111 - HLT
};
__uint16_t extra_instructions[16 * 8][8] = {
{ 0, 0, 0, 0, 0, 0, 0, 0}, //0000 - NOP
{ 0, 0, 0, 0, 0, 0, 0, 0}, //0001 - LDA
{ 0, 0, 0, 0, FI, 0, 0, 0}, //0010 - ADD
{ 0, 0, 0, 0, FI, 0, 0, 0}, //0011 - SUBS
{ 0, 0, 0, 0, 0, 0, 0, 0}, //0100 - JMP
{ 0, 0, 0, 0, 0, 0, 0, 0}, //0101 - JZ
{ 0, 0, 0, 0, 0, 0, 0, 0}, //0110 - JC
{ 0, 0, SI, 0, 0, 0, 0, 0}, //0111 - LDS
{ 0, 0, RNDO, 0, 0, 0, 0, 0}, //1000 - RND
{ 0, 0, 0, 0, 0, 0, 0, 0}, //1001 - STORE
{ 0, 0, 0, 0, 0, 0, 0, 0}, //1010 - JNI
{ 0, 0, FI, 0, 0, 0, 0, 0}, //1011 - READ
{ 0, 0, TTYOUT, 0, 0, 0, 0, 0}, //1100 - PRINT
{ 0, 0, TTYC|KBC, 0, 0, 0, 0, 0}, //1101 - CLS
{ 0, 0, 0, 0, 0, 0, 0, 0}, //1110 - NS
{ 0, 0, 0, 0, 0, 0, 0, 0}, //1111 - HLT
};
int main(void) {
FILE *rom1 = fopen("MICROCODE1","w+");
FILE *rom2 = fopen("MICROCODE2","w+");
for (int i = 0; i < 16; ++i) {
for(int k = 1; k < 8; ++k){
memcpy(instructions[i|(k << 4)],instructions[i],sizeof(instructions[0]));
memcpy(extra_instructions[i|(k << 4)],extra_instructions[i],sizeof(extra_instructions[0]));
}
}
instructions[JZ|(ZF)][2] = IO|J;
instructions[JZ|(ZF|IF)][2] = IO|J;
instructions[JZ|(ZF|CF)][2] = IO|J;
instructions[JZ|(ZF|CF|IF)][2] = IO|J;
instructions[JC|(CF)][2] = IO|J;
instructions[JC|(CF|IF)][2] = IO|J;
instructions[JC|(CF|ZF)][2] = IO|J;
instructions[JC|(CF|ZF|IF)][2] = IO|J;
instructions[JNI|(IF)][2] = 0;
instructions[JNI|(IF|ZF)][2] = 0;
instructions[JNI|(IF|CF)][2] = 0;
instructions[JNI|(IF|CF|ZF)][2] = 0;
instructions[READ|(IF)][2] = AI;
instructions[READ|(IF|ZF)][2] = AI;
instructions[READ|(IF|CF)][2] = AI;
instructions[READ|(IF|CF|ZF)][2] = AI;
extra_instructions[READ|(IF)][2] = KBO|FI;
extra_instructions[READ|(IF|ZF)][2] = KBO|FI;
extra_instructions[READ|(IF|CF)][2] = KBO|FI;
extra_instructions[READ|(IF|CF|ZF)][2] = KBO|FI;
printf("====ROM1====\n");
for (int j = 0; j < sizeof(instructions)/sizeof(instructions[0]); ++j) {
printf("%07b: ",j);
for (int i = 0; i < 8; ++i) {
// ReSharper disable once CppPrintfBadFormat
printf("%016b ",instructions[j][i]);
}
printf("\n");
}
printf("====ROM1====\n");
printf("====ROM2====\n");
for (int j = 0; j < sizeof(extra_instructions)/sizeof(extra_instructions[0]); ++j) {
printf("%07b: ",j);
for (int i = 0; i < 8; ++i) {
// ReSharper disable once CppPrintfBadFormat
printf("%016b ",extra_instructions[j][i]);
}
printf("\n");
}
printf("====ROM2====\n");
fwrite(instructions,sizeof(instructions),1,rom1);
fwrite(extra_instructions,sizeof(extra_instructions),1,rom2);
fclose(rom1);
fclose(rom2);
return 0;
}