This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
forked from k1rill-fedoseev/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.v
257 lines (232 loc) · 4.33 KB
/
main.v
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
module main(
//Just 50 MHz clock
input clock,
//Reset signal
input reset,
//Representation switch
input show_in_hex,
//Show stack elements count switch
input show_count,
//Button, switches to operations keyboard
input alt_numpad_key,
//Alternative keyboard indicator
output alt_numpad_led,
//Numpad rows and columns
input [3:0] numpad_rows,
output [3:0] numpad_columns,
//Display and display control
output [7:0] display_leds,
output [7:0] display_control
);
// 1 2 3 C
// 4 5 6 PUSH
// 7 8 9 POP
// 0 +- CE SWAP
parameter BTN_0 = 6'b110011;
parameter BTN_1 = 6'b110000;
parameter BTN_2 = 6'b110100;
parameter BTN_3 = 6'b111000;
parameter BTN_4 = 6'b110001;
parameter BTN_5 = 6'b110101;
parameter BTN_6 = 6'b111001;
parameter BTN_7 = 6'b110010;
parameter BTN_8 = 6'b110110;
parameter BTN_9 = 6'b111010;
parameter BTN_CLEAR_DIGIT = 6'b111100;
parameter BTN_PUSH = 6'b111101;
parameter BTN_POP = 6'b111110;
parameter BTN_SWAP = 6'b111111;
parameter BTN_CLEAR_NUMBER = 6'b111011;
parameter BTN_UNARY_MINUS = 6'b110111;
// + - * /
// sqr cbe inc dec
parameter BTN_ADDITION = 6'b100000;
parameter BTN_SUBTRACTION = 6'b100100;
parameter BTN_MULTIPLICATION = 6'b101000;
parameter BTN_DIVISION = 6'b101100;
parameter BTN_SQUARE = 6'b100001;
parameter BTN_CUBE = 6'b100101;
parameter BTN_INCREMENT = 6'b101001;
parameter BTN_DECREMENT = 6'b101101;
//Numpad state
wire [5:0] pressed;
//Stack elements count
wire [5:0] count;
//First and second stack elements
wire [31:0] top, next;
wire stack_error;
//Evaluated new value
reg [31:0] new_value;
//Stack control signals
reg write, push, pop, swap;
reg arithmetic_error = 0;
numpad numpad(
.clock (clock),
.alt_key (~alt_numpad_key),
.alt_led (alt_numpad_led),
.rows (numpad_rows),
.columns (numpad_columns),
.value (pressed)
);
stack stack(
.clock (clock),
.reset (~reset),
.push (push),
.pop (pop),
.swap (swap),
.write (write),
.value (new_value),
.top (top),
.next (next),
.count (count),
.error (stack_error)
);
display_bcd display(
.clock (clock),
.error (stack_error || arithmetic_error),
.show_in_hex (show_in_hex),
.value (show_count ? count : top),
.control (display_control),
.leds (display_leds)
);
// Division result
wire [31:0] res;
assign res = ((next[31] ? -next : next) / (top[31] ? -top : top));
always @(posedge clock)
begin
//Reseting arithmetic error
if (~reset)
arithmetic_error <= 0;
case (pressed)
BTN_0:
begin
write <= 1;
new_value <= top * 10;
end
BTN_1:
begin
write <= 1;
new_value <= top * 10 + (top[31] ? -1 : 1);
end
BTN_2:
begin
write <= 1;
new_value <= top * 10 + (top[31] ? -2 : 2);
end
BTN_3:
begin
write <= 1;
new_value <= top * 10 + (top[31] ? -3 : 3);
end
BTN_4:
begin
write <= 1;
new_value <= top * 10 + (top[31] ? -4 : 4);
end
BTN_5:
begin
write <= 1;
new_value <= top * 10 + (top[31] ? -5 : 5);
end
BTN_6:
begin
write <= 1;
new_value <= top * 10 + (top[31] ? -6 : 6);
end
BTN_7:
begin
write <= 1;
new_value <= top * 10 + (top[31] ? -7 : 7);
end
BTN_8:
begin
write <= 1;
new_value <= top * 10 + (top[31] ? -8 : 8);
end
BTN_9:
begin
write <= 1;
new_value <= top * 10 + (top[31] ? -9 : 9);
end
BTN_CLEAR_DIGIT:
begin
write <= 1;
new_value <= top / 10;
end
BTN_CLEAR_NUMBER:
begin
write <= 1;
new_value <= 0;
end
BTN_PUSH:
begin
push <= 1;
end
BTN_POP:
begin
pop <= 1;
end
BTN_SWAP:
begin
swap <= 1;
end
BTN_UNARY_MINUS:
begin
write <= 1;
new_value <= -top;
end
BTN_ADDITION:
begin
pop <= 1;
write <= 1;
new_value <= next + top;
end
BTN_SUBTRACTION:
begin
pop <= 1;
write <= 1;
new_value <= next - top;
end
BTN_MULTIPLICATION:
begin
pop <= 1;
write <= 1;
new_value <= next * top;
end
BTN_DIVISION:
begin
pop <= 1;
write <= 1;
new_value <= (next[31] ^ top[31] ? -res : res);
arithmetic_error <= ~(|top);
end
BTN_SQUARE:
begin
write <= 1;
new_value <= top * top;
end
BTN_CUBE:
begin
write <= 1;
new_value <= top * top * top;
end
BTN_INCREMENT:
begin
write <= 1;
new_value <= top + 1;
end
BTN_DECREMENT:
begin
write <= 1;
new_value <= top - 1;
end
default: // Nothing usefull is pressed
begin
write <= 0;
push <= 0;
pop <= 0;
swap <= 0;
end
endcase
end
endmodule