少女祈祷中...

核心功能与创新功能(PPT)


代码汇总

板卡顶层模块AP600.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
// AP600: Interface between top-moudule of calculator and FPGA board
module AP600 (clk, reset, pswA, pswB, pswC, pswD, dipA, dipB,
hexA, hexB, buzzer, ledA, ledB, ledC, ledD,
segA, segB, segC, segD, segE, segF, segG, segH) ;

input clk, reset; // Clock, Reset
input[4:0] pswA, pswB, pswC, pswD; // Push switch
input[7:0] dipA, dipB; // DIP switch
input[3:0] hexA, hexB; // Rotary switch
output buzzer; // Buzzer
output[7:0] ledA, ledB, ledC, ledD;// LED
output[7:0] segA, segB, segC, segD, segE, segF, segG, segH ; // 7SEG LED

wire [7:0] ledb,ledc,ledd,lede,ledf,ledg,ledh;
wire [9:0] push;
wire overflow, sign, ce, plus, minus, equal;

// for Debug
wire [1:0] state;
wire [24:0] regb;
wire [24:0] rega;
wire [3:0] count;
wire [3:0] opr;

assign push[0] = pswD[0];
assign push[1] = pswC[0];
assign push[2] = pswC[1];
assign push[3] = pswC[2];
assign push[4] = pswB[0];
assign push[5] = pswB[1];
assign push[6] = pswB[2];
assign push[7] = pswA[0];
assign push[8] = pswA[1];
assign push[9] = pswA[2];
assign plus = pswD[3]; //nAk
assign minus = pswC[3]; //nCk
assign mult = pswB[3]; //x!
assign div = pswA[3]; //x^2
assign ce = pswA[4];
assign equal = pswD[4];
assign switch = pswC[4];
assign inverse = pswD[2]; //±
assign game = pswD[1]; //进入井字棋游玩模式

// Output assignment
assign buzzer = overflow;

assign ledA = {overflow,2'b00, count[0],count[1],
opr, state[0], state[1]};
assign ledB = {regb[8], 7'b0000000};
assign ledC = {regb[0],regb[1],regb[2],regb[3],
regb[4],regb[5],regb[6],regb[7]};
assign ledD = {rega[0],rega[1],rega[2],rega[3],
rega[4],rega[5],rega[6],1'b0};

//H最低位,B最高位!!! (A符号位)
assign segA = {6'b000000,sign,1'b0};
assign segB = ledb;
assign segC = ledc;
assign segD = ledd;
assign segE = lede;
assign segF = ledf;
assign segG = ledg;
assign segH = ledh;

calctop calctop(clk, reset, push, ce, plus, minus, mult, div, equal, switch, inverse,
sign, ledh, ledg, ledf, lede, ledd, ledc, ledb, overflow, state, rega, regb,
count, opr, game);

endmodule

运算和显示顶层模块calctop.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
// Calctop: Calculator top module
module calctop(clk, reset, push, ce, plus, minus, mult, div, equal, switch, inverse,
sign, ledh, ledg, ledf, lede, ledd, ledc, ledb, overflow, state, rega, regb,
count, opr, game);

input plus, minus, equal, ce, reset, clk;
//新增按键
input mult, div, switch, inverse;
input game;
input [9:0] push;
output overflow, sign;
//output [7:0] ledh, ledl;
output [7:0] ledh, ledg, ledf, lede, ledd, ledc, ledb;

// for Debug
output [1:0] state;
output [24:0] regb;
output [24:0] rega;
output [3:0] count;
output [3:0] opr;

wire plusout, minusout, equalout, ceout;
// 新增按键
wire multout, divout;
wire switchout, inverseout;
//
wire [9:0] pushout;
wire [24:0] wout;

calc calc(pushout, plusout, minusout, multout, divout, equalout, switchout, inverseout, clk, reset, ceout,
sign, overflow, wout, state, rega, regb, count,
opr, gameout);

//binled binled(wout, ledl, ledh);
binled binled(wout, ledh, ledg, ledf, lede, ledd, ledc, ledb);

syncro syncroce(ceout, ce, clk, reset);
syncro syncropuls(plusout, plus, clk, reset);
syncro syncrominus(minusout, minus, clk, reset);
syncro syncroequal(equalout, equal, clk, reset);
//新增按键
syncro syncromult(multout, mult, clk, reset);
syncro syncrodiv(divout, div, clk, reset);
syncro syncroswitch(switchout, switch, clk, reset);
syncro syncroinverse(inverseout, inverse, clk, reset);
syncro syncrogame(gameout, game, clk, reset);

syncro10 syncropush(pushout, push, clk, reset);

endmodule

核心模块calc.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
`define DECIMAL 0
`define OPE 1
`define HALT 2

`define XY 3 //运算x^y
`define DOUBLEJC 4 //运算x!!
`define GCD 5 //最大公因数
`define LCM 6 //最小公倍数
`define MatPre 7 //预处理
`define MatPlus 8 //矩阵加
`define MatHLS 9 //行列式的值
`define MatMult 10 //矩阵乘
`define Cov1D 11 //1维序列线性卷积

`define P1 12 //玩家一按
`define P2 13 //玩家二按
`define Judge 14 //判断输赢

// Calc: Calculator main module
module calc(decimal, plus, minus, mult, div, equal, switch, inverse, clk, reset,
ce, sign, overflow, out, state, REGA, REGB, count, opr, game);
input [9:0] decimal;
input clk, ce, reset, plus, minus, equal;
input mult,div;
input switch, inverse;
input game;
output sign, overflow;
output [24:0] out;

// for Debugging
output [3:0] state;
output [24:0] REGB;
output [24:0] REGA;
output [3:0] count; //0-7bit
//output add_or_sub;
output [3:0] opr;

wire [3:0] d;
//wire [8:0] alu_out;
reg [3:0] state;
//七位数:-9999999~9999999//
reg [24:0] REGB; //七位数最多9999999=24bit ,开设25bit寄存器,最高位是负数指示位//
reg [24:0] REGA;
reg [3:0] count;
//reg add_or_sub ;
reg [3:0] opr;

function [3:0] dectobin;
input [9:0] in;
if(in[9])
dectobin = 9;
else if(in[8])
dectobin = 8;
else if(in[7])
dectobin = 7;
else if(in[6])
dectobin = 6;
else if(in[5])
dectobin = 5;
else if(in[4])
dectobin = 4;
else if(in[3])
dectobin = 3;
else if(in[2])
dectobin = 2;
else if(in[1])
dectobin = 1;
else if(in[0])
dectobin = 0;
endfunction

assign d=dectobin(decimal);

//阶乘表JC_MAP
wire[24:0] JC_MAP[10:0];
assign JC_MAP[0] = 25'd1; // 0! = 1
assign JC_MAP[1] = 25'd1; // 1! = 1
assign JC_MAP[2] = 25'd2; // 2! = 2
assign JC_MAP[3] = 25'd6; // 3! = 6
assign JC_MAP[4] = 25'd24; // 4! = 24
assign JC_MAP[5] = 25'd120; // 5! = 120
assign JC_MAP[6] = 25'd720; // 6! = 720
assign JC_MAP[7] = 25'd5040; // 7! = 5040
assign JC_MAP[8] = 25'd40320; // 8! = 40320
assign JC_MAP[9] = 25'd362880; // 9! = 362880
assign JC_MAP[10] = 25'd3628800; // 10! = 3628800

reg[3:0] s_cnt; //switch按下计数
reg[24:0] x,y;
reg[24:0] a,b;
reg BranchFSM_running; //正在运算旁支FSM:XY
integer i;
integer DOUBLE_JC_MAX = 16;
integer XY_MAX = 10; //x^y中y的最大值
//矩阵运算
reg[2:0] Mat_weishu;
reg over;
reg[3:0] Mnumber6A,Mnumber5A,Mnumber4A,Mnumber3A,Mnumber2A,Mnumber1A; //矩阵A
reg[3:0] Mnumber6B,Mnumber5B,Mnumber4B,Mnumber3B,Mnumber2B,Mnumber1B; //矩阵B
reg[3:0] Mnumber7R,Mnumber6R,Mnumber5R,Mnumber4R,Mnumber3R,Mnumber2R,Mnumber1R; //结果矩阵R
//井字棋
reg[2:0] L_1,M_1,H_1; //玩家一的三行输入情况[H,M,L]
reg[2:0] L_2,M_2,H_2; //玩家二的三行输入情况
reg[3:0] Pcnt; //玩家步数计数
reg[1:0] winner;


always @(posedge clk or negedge reset)
begin
if(!reset)
begin
REGA <= 0; REGB <= 0; count <= 0;
x <= 0; y <= 0; a <= 0; b <= 0;
BranchFSM_running <= 0;
Mat_weishu <= 0;
over <= 0;
Mnumber6A <= 0; Mnumber5A<= 0; Mnumber4A<= 0; Mnumber3A<= 0; Mnumber2A<= 0; Mnumber1A<= 0;
Mnumber6B <= 0; Mnumber5B<= 0; Mnumber4B<= 0; Mnumber3B<= 0; Mnumber2B<= 0; Mnumber1B<= 0;
Mnumber7R <= 0; Mnumber6R <= 0; Mnumber5R<= 0; Mnumber4R<= 0; Mnumber3R<= 0; Mnumber2R<= 0; Mnumber1R<= 0;
L_1 <= 0; M_1 <= 0; H_1 <= 0;
L_2 <= 0; M_2 <= 0; H_2 <= 0;
Pcnt <= 0;
winner <= 0;
opr <= 0;
s_cnt <= 0;
state<= `DECIMAL;
end
else
begin
case (state)
`DECIMAL :
begin
//switch计数:0 -> 1 -> 2
s_cnt <= (switch==1) ? (s_cnt+1) : s_cnt;

if((decimal != 0) && (count < 7)) //修改位数
begin
count <= count + 1;
REGA <= REGA * 10 + d;
end
else if(ce)
begin
REGA <= 0;
count <= 0;
end
else if(inverse) //±
begin
REGA <= ~REGA + 1;
end
else if (game) begin //按下game按键
state <= `P1;
REGA <= 1234321;
REGB <= 1234321;
end
//A: 7 8 9 / CE
//B: 4 5 6 * C
//C: 1 2 3 - S
//D: 0 ± + = switch=0 4 8
//-----------------------------
//A: 7 8 9 x^2 CE
//B: 4 5 6 x! C
//C: 1 2 3 mCn S
//D: 0 ± mAn = switch=1 5 9
//-----------------------------
//A: 7 8 9 x^y CE
//B: 4 5 6 x!! C
//C: 1 2 3 GCD S
//D: 0 ± LCM = switch=2 6 10
//-----------------------------
//A: 7 8 9 Mat+ CE
//B: 4 5 6 |Mat| C
//C: 1 2 3 Mat* S
//D: 0 ± 1D-cov = switch=3 7 11
//-----------------------------
else if(plus || minus || mult || div || equal)
begin
if(s_cnt == 0 || s_cnt % 4 == 0) begin
case(opr)
4'b0000: REGB <= REGB + REGA;
4'b0001: REGB <= REGB - REGA;
4'b0010: REGB <= REGB * REGA;
4'b0011: REGB <= REGB / REGA;
endcase

if(plus) opr <= 4'b0000;
else if(minus) opr <= 4'b0001;
else if(mult) opr <= 4'b0010;
else if(div) opr <= 4'b0011;
else opr <= opr;
end //
else if(s_cnt % 4 == 1) begin
case(opr)
4'b0000: REGB <= REGB + REGA;
4'b0001: REGB <= JC_MAP[REGB] / JC_MAP[REGB - REGA]; //nAk: n个里选择k个 n!/(n-k)!
4'b0010: REGB <= JC_MAP[REGB] / (JC_MAP[REGA] * JC_MAP[REGB - REGA]); //nCk
4'b0011: REGB <= JC_MAP[REGB];
4'b0100: REGB <= REGB * REGB;
endcase

if(plus) opr <= 4'b0001; //nAk
else if(minus) opr <= 4'b0010; //nCk
else if(mult) opr <= 4'b0011; //x!
else if(div) opr <= 4'b0100; //x^2
else opr <= opr;
end
else if(s_cnt % 4 == 2) begin
case(opr)
4'b0000: REGB <= REGB + REGA;
4'b0100: begin //x^y
x <= REGB;
y <= REGA;
state <= `XY;
end
4'b0011: begin //x!! 输入必须按两次一样的数, 比如5!!就要按5!!5
x <= REGB;
y <= REGB;
state <= `DOUBLEJC;
end
4'b0010: begin //GCD辗转相除法: x=max(REGA,REGB),y=min(REGA,REGB), (x,y)<-(y,x % y)
x <= (REGA > REGB) ? REGA : REGB;
y <= (REGA < REGB) ? REGA : REGB;
state <= `GCD;
end
4'b0001: begin //LCM = (a * b) /GCD
x <= (REGA > REGB) ? REGA : REGB;
y <= (REGA < REGB) ? REGA : REGB;
state <= `LCM;
end
endcase

if(plus) opr <= 4'b0001; //LCM
else if(minus) opr <= 4'b0010; //GCD
else if(mult) opr <= 4'b0011; //x!!
else if(div) opr <= 4'b0100; //x^y
else opr <= opr;
end
else if(s_cnt % 4 == 3) begin
case(opr)
4'b0000: REGB <= REGB + REGA;
//支持的矩阵大小: 1*2 2*2 3*2
//逐行展平:比如矩阵[ [2,3,1] ; [1,4,8] ]即输入231148
//首先前往MatPre状态预处理,然后分别前往MatPlus,MatMult和hls
4'b0001: begin //一维离散序列线性卷积
//state <= `Cov1D;
state <= `MatPre;
end
4'b0010: begin
//state <= `MatMult;
state <= `MatPre;
end
4'b0011: begin
//state <= `MatHLS;
state <= `MatPre;
end
4'b0100: begin
//state <= `MatPlus;
state <= `MatPre;
end
endcase

if(plus) opr <= 4'b0001; //1D-cov
else if(minus) opr <= 4'b0010; //Mat*
else if(mult) opr <= 4'b0011; //|Mat|
else if(div) opr <= 4'b0100; //Mat+
else opr <= opr;
end


if(BranchFSM_running == 0) begin //!!!!!!!!!!!!!!!!!!!!!!!!!
state <= `OPE;
REGA <= 0;
end
end
end

`OPE: begin
if (((REGB[24]==1)&&(REGB<16777216)) //最大负数:1+ 24个0 是能表示的最大负数
|| ((REGB[24]==0)&&(REGB>9999999)))
state<=`HALT;
else if(inverse)
REGB <= ~REGB + 1;
else if(decimal) begin
REGA <= d; //新输入
count <= 1;
state <= `DECIMAL;
end

if(s_cnt % 4 == 2 || s_cnt % 4 == 3) //正在执行DECIMAL->XY/lcm/gcd/Mat的旁支FSM,运算未结束时暂时不要跳转到OPE!!!!!!!!!
BranchFSM_running <= 1;
end

`HALT:
if(ce) begin
REGA <= 0;
REGB <= 0;
opr <= 0;
count <= 0;
state <= `DECIMAL;
end

//旁支FSM:负责运算X^Y,x!!,GCD,LCM ----------------------------------------------------------------------------------------------------
`XY: begin
y <= y - 1;
REGB <= (y == 1) ? REGB : REGB * x;
state <= (y == 1) ? `OPE : `XY;
end
`DOUBLEJC: begin
y <= y - 2;
if(y == REGB) //第一次
REGB <= REGB;
else
REGB <= (y <= 1) ? REGB : REGB * y;

state <= (y <= 1) ? `OPE : `DOUBLEJC;
end
`GCD: begin //GCD辗转相除法: x=max(REGA,REGB),y=min(REGA,REGB), (x,y)<-(y,x % y)
if(x % y == 0) begin
REGB <= y;
state <= `OPE;
end
else begin
x <= y;
y <= x % y;
state <= `GCD;
end
end
`LCM: begin //LCM = (a * b) /GCD
if(x % y == 0) begin
REGB <= (REGA * REGB) / y;
state <= `OPE;
end
else begin
x <= y;
y <= x % y;
state <= `LCM;
end
end
//矩阵操作----------------------------------
`MatPre: begin
//检测位数
if(REGB> 99999) begin //六位数:2*3
Mat_weishu <= 6;
end
else if(REGB > 9999 && REGB < 100000) begin //五位数:2*3,最高位补零
Mat_weishu <= 5;
end
else if(REGB > 999 && REGB < 10000) begin //四位数: 2*2
Mat_weishu <= 4;
end
else if(REGB > 99 && REGB < 1000) begin //三位数: 2*2, 最高位补零
Mat_weishu <= 3;
end
else if(REGB >9 && REGB < 100) begin //二位数:1*2
Mat_weishu <= 2;
end
else if(REGB >=0 && REGB < 10) begin //一位数: 1*2 ,最高位补0
Mat_weishu <= 1;
end
else begin
Mat_weishu <= 0;
end
//分割数字
Mnumber6B <= REGB / 100000;
Mnumber6A <= REGA / 100000;
Mnumber5B <= (REGB % 100000) / 10000;
Mnumber5A <= (REGA % 100000) / 10000;
Mnumber4B <= (REGB % 10000) / 1000;
Mnumber4A <= (REGA % 10000) / 1000;
Mnumber3B <= (REGB % 1000) / 100;
Mnumber3A <= (REGA % 1000) / 100;
Mnumber2B <= (REGB % 100) / 10;
Mnumber2A <= (REGA % 100) / 10;
Mnumber1B <= (REGB % 10) / 1;
Mnumber1A <= (REGA % 10) / 1;
//预处理结束,进入对应运算FSM
case(opr)
4'b0001: state <= `Cov1D;
4'b0010: state <= `MatMult;
4'b0011: state <= `MatHLS;
4'b0100: state <= `MatPlus;
endcase
end

`MatPlus: begin
case(Mat_weishu)
6: begin //M2*3
Mnumber1R <= Mnumber1A + Mnumber1B;
Mnumber2R <= Mnumber2A + Mnumber2B;
Mnumber3R <= Mnumber3A + Mnumber3B;
Mnumber4R <= Mnumber4A + Mnumber4B;
Mnumber5R <= Mnumber5A + Mnumber5B;
Mnumber6R <= Mnumber6A + Mnumber6B;
over <= 1;
end
5: begin
//最高位补0,但不用写
Mnumber1R <= Mnumber1A + Mnumber1B;
Mnumber2R <= Mnumber2A + Mnumber2B;
Mnumber3R <= Mnumber3A + Mnumber3B;
Mnumber4R <= Mnumber4A + Mnumber4B;
Mnumber5R <= Mnumber5A + Mnumber5B;
over <= 1;
end
4: begin //M2*2
Mnumber1R <= Mnumber1A + Mnumber1B;
Mnumber2R <= Mnumber2A + Mnumber2B;
Mnumber3R <= Mnumber3A + Mnumber3B;
Mnumber4R <= Mnumber4A + Mnumber4B;
over <= 1;
end
3: begin
Mnumber1R <= Mnumber1A + Mnumber1B;
Mnumber2R <= Mnumber2A + Mnumber2B;
Mnumber3R <= Mnumber3A + Mnumber3B;
over <= 1;
end
2: begin //M1*2
Mnumber1R <= Mnumber1A + Mnumber1B;
Mnumber2R <= Mnumber2A + Mnumber2B;
over <= 1;
end
1: begin
Mnumber1R <= Mnumber1A + Mnumber1B;
over <= 1;
end
endcase

if(over == 1) begin
REGB <= Mnumber6R * 100000 + Mnumber5R * 10000 + Mnumber4R * 1000 + Mnumber3R * 100 + Mnumber2R * 10 + Mnumber1R * 1;
state <= `OPE;
over <= 0;
end
else
state <= `MatPlus;
end

`MatHLS: begin
//只有2*2方阵有行列式:|abcd| = ad - bc
if(Mat_weishu != 4 && Mat_weishu != 3) begin
REGB <= 9999999; //没有逆矩阵显示9999999
state <= `OPE;
end
else begin
REGB <= Mnumber4B * Mnumber1B - Mnumber2B * Mnumber3B;
state <= `OPE;
end
end

`MatMult: begin
//运算
//1*2-REGB[a,b] 与 2*1-REGA[c;d] 相乘为 1*1[ac+bd]
if(Mat_weishu == 2) begin
Mnumber1R <= Mnumber2B * Mnumber2A + Mnumber1B * Mnumber1A;
over <= 1;
//REGB <= Mnumber1R;
end
//2*2-REGB[[a,b];[c,d]] 与 2*2-REGA[[e,f];[g,h]] 相乘为 2*2[[ae+bg],[af+bh];[ce+dg],[cf+dh]]
else if(Mat_weishu == 4 || Mat_weishu == 3) begin
Mnumber4R <= Mnumber4B * Mnumber4A + Mnumber3B * Mnumber2A; //ae+bg
Mnumber3R <= Mnumber4B * Mnumber3A + Mnumber3B * Mnumber1A; //af+bh
Mnumber2R <= Mnumber2B * Mnumber4A + Mnumber1B * Mnumber2A; //ce+dg
Mnumber1R <= Mnumber2B * Mnumber3A + Mnumber1B * Mnumber1A; //cf+dh
over <= 1;
//REGB <= Mnumber4R * 1000 + Mnumber3R * 100 + Mnumber2R * 10 + Mnumber1R * 1;
end
//2*3-REGB[[a,b,c];[d,e,f]] 与 3*2-REGA[[g,h];[i,j];[k,l]] 相乘为 2*2[[ag+bi+ck,ah+bj+cl];[dg+ei+fk,dh+ej+fl]]
else if(Mat_weishu == 6 || Mat_weishu == 5) begin
Mnumber4R <= Mnumber6B * Mnumber6A + Mnumber5B * Mnumber4A + Mnumber4B * Mnumber2A; //ag+bi+ck
Mnumber3R <= Mnumber6B * Mnumber5A + Mnumber5B * Mnumber3A + Mnumber4B * Mnumber1A; //ah+bj+cl
Mnumber2R <= Mnumber3B * Mnumber6A + Mnumber2B * Mnumber4A + Mnumber1B * Mnumber2B; //dg+ei+fk
Mnumber1R <= Mnumber3B * Mnumber5A + Mnumber2B * Mnumber3A + Mnumber1B * Mnumber1B; //dh+ej+fl
over <= 1;
//REGB <= Mnumber4R * 1000 + Mnumber3R * 100 + Mnumber2R * 10 + Mnumber1R * 1;
end
else //不支持的运算
REGB <= 9999999;

//结果赋予REGB
if(over == 1) begin
REGB <= Mnumber4R * 1000 + Mnumber3R * 100 + Mnumber2R * 10 + Mnumber1R * 1;
over <= 0;
state <= `OPE;
end
else begin
state <= `MatMult;
end
end

`Cov1D: begin
//N长序列 和 M长序列 卷积结果是 M+N-1。最高不能超过7,否则无法显示
//直接规定最大 N=4 * M=4
if(Mat_weishu > 4) begin
REGB <= 9999999;
over <= 1;
end
else begin
Mnumber7R <= Mnumber4B * Mnumber1A;
Mnumber6R <= Mnumber3B * Mnumber1A + Mnumber4B * Mnumber2A;
Mnumber5R <= Mnumber2B * Mnumber1A + Mnumber3B * Mnumber2A + Mnumber4B * Mnumber3A;
Mnumber4R <= Mnumber1B * Mnumber1A + Mnumber2B * Mnumber2A + Mnumber3B * Mnumber3A + Mnumber4B * Mnumber4A;
Mnumber3R <= Mnumber3B * Mnumber4A + Mnumber2B * Mnumber3A + Mnumber1B * Mnumber2A;
Mnumber2R <= Mnumber2B * Mnumber4A + Mnumber1B * Mnumber3A;
Mnumber1R <= Mnumber1B * Mnumber4A;
over <= 1;
end

if(over == 1) begin
REGB <= Mnumber7R * 1000000 + Mnumber6R * 100000 + Mnumber5R * 10000 + Mnumber4R * 1000 + Mnumber3R * 100 + Mnumber2R * 10 + Mnumber1R * 1;
state <= `OPE;
end
else begin
state <= `Cov1D;
end
end

//井字棋--------------------------------------------------
`P1: begin
if((decimal != 0)) begin //键入
H_1[0] <= (d == 7) ? (H_1[0]+ 1) : H_1[0];
H_1[1] <= (d == 8) ? (H_1[1]+ 1) : H_1[1];
H_1[2] <= (d == 9) ? (H_1[2]+ 1) : H_1[2];
M_1[0] <= (d == 4) ? (M_1[0]+ 1) : M_1[0];
M_1[1] <= (d == 5) ? (M_1[1]+ 1) : M_1[1];
M_1[2] <= (d == 6) ? (M_1[2]+ 1) : M_1[2];
L_1[0] <= (d == 1) ? (L_1[0]+ 1) : L_1[0];
L_1[1] <= (d == 2) ? (L_1[1]+ 1) : L_1[1];
L_1[2] <= (d == 3) ? (L_1[2]+ 1) : L_1[2];
Pcnt <= Pcnt + 1;
state <= `Judge;
end
else begin
state <= `P1;
end
REGB <= 1110000;
end
`P2: begin
if((decimal != 0)) begin //键入
H_2[0] <= (d == 7) ? (H_2[0]+1) : H_2[0];
H_2[1] <= (d == 8) ? (H_2[1]+1) : H_2[1];
H_2[2] <= (d == 9) ? (H_2[2]+1) : H_2[2];
M_2[0] <= (d == 4) ? (M_2[0]+1) : M_2[0];
M_2[1] <= (d == 5) ? (M_2[1]+1) : M_2[1];
M_2[2] <= (d == 6) ? (M_2[2]+1) : M_2[2];
L_2[0] <= (d == 1) ? (L_2[0]+1) : L_2[0];
L_2[1] <= (d == 2) ? (L_2[1]+1) : L_2[1];
L_2[2] <= (d == 3) ? (L_2[2]+1) : L_2[2];
Pcnt <= Pcnt + 1;
state <= `Judge;
end
else begin
state <= `P2;
end
REGB <= 111;
end
`Judge: begin
if( H_1 == 3'b111 || M_1 == 3'b111 || L_1 == 3'b111 ||
{H_1[0],M_1[0],L_1[0]} == 3'b111 || {H_1[1],M_1[1],L_1[1]} == 3'b111 || {H_1[2],M_1[2],L_1[2]} == 3'b111 ||
{H_1[0],M_1[1],L_1[2]} == 3'b111 || {H_1[2],M_1[1],L_1[0]} == 3'b111 ) begin
winner <= 1; //玩家一胜利
REGB <= 1111111;
state <= `OPE;
end
else if( H_2 == 3'b111 || M_2 == 3'b111 || L_2 == 3'b111 ||
{H_2[0],M_2[0],L_2[0]} == 3'b111 || {H_2[1],M_2[1],L_2[1]} == 3'b111 || {H_2[2],M_2[2],L_2[2]} == 3'b111 ||
{H_2[0],M_2[1],L_2[2]} == 3'b111 || {H_2[2],M_2[1],L_2[0]} == 3'b111 ) begin
winner <= 2; //玩家二胜利
REGB <= 2222222;
state <= `OPE;
end
else begin //目前没决出胜负或者平
if(Pcnt == 9) begin //下完了但是平局
winner <= 0;
REGB <= 5555555;
state <= `OPE;
end
else begin //未决出胜负,继续下
state <= (Pcnt % 2 == 0) ? `P1 : `P2;
end
end
end

//More States 》》》

endcase
end
end

assign overflow=(state==`HALT)?1:0;
//assign sign=(state==`DECIMAL)? 0: ((state==`OPE)?(REGB[24]) :0);
assign sign=(state==`DECIMAL)? REGA[24]: ((state==`OPE)?(REGB[24]) :0);
assign out=out_func (state, REGA, REGB);

function [24:0] out_func;
input [1:0] s; input [24:0] a; input [24:0] b;
case(s)
`DECIMAL :
out_func = a;

`OPE :
if(b[24]==1) //负数:检查最高位
out_func = ~b + 1;
else
out_func = b;
endcase
endfunction

endmodule

八段数码管显示顶层模块binled.v

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module binled(in, ledh, ledg, ledf, lede, ledd, ledc, ledb);
input [24:0] in;
output [7:0] ledh,ledg,ledf,lede,ledd,ledc,ledb;
wire [3:0] outh,outg,outf,oute,outd,outc,outb;

bintobcd bintobcd(in,outh,outg,outf,oute,outd,outc,outb);
ledout ledouth(outh, ledh);
ledout ledoutg(outg, ledg);
ledout ledoutf(outf, ledf);
ledout ledoute(oute, lede);
ledout ledoutd(outd, ledd);
ledout ledoutc(outc, ledc);
ledout ledoutb(outb, ledb);

endmodule

二进制转BCD码模块bintobcd.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
module bintobcd(in,outh,outg,outf,oute,outd,outc,outb) ;
input [24:0] in;
output [3:0] outh,outg,outf,oute,outd,outc,outb;
wire [24:0] temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10, temp11, temp12,
temp13, temp14, temp15, temp16, temp17, temp18, temp19, temp20, temp21, temp22, temp23, temp24;

//百万位b
assign outb[3] = (in >= 8000000) ? 1 : 0;
assign temp1 = (in >= 8000000) ? in - 8000000 : in;
assign outb[2] = (temp1 >= 4000000) ? 1 : 0;
assign temp2 = (temp1 >= 4000000) ? temp1 - 4000000 : temp1;
assign outb[1] = (temp2 >= 2000000) ? 1 : 0;
assign temp3 = (temp2 >= 2000000) ? temp2 - 2000000 : temp2;
assign outb[0] = (temp3 >= 1000000) ? 1 : 0;
assign temp4 = (temp3 >= 1000000) ? temp3 - 1000000 : temp3;
//十万位c
assign outc[3] = (temp4 >= 800000) ? 1 : 0;
assign temp5 = (temp4 >= 800000) ? temp4 - 800000 : temp4;
assign outc[2] = (temp5 >= 400000) ? 1 : 0;
assign temp6 = (temp5 >= 400000) ? temp5 - 400000 : temp5;
assign outc[1] = (temp6 >= 200000) ? 1 : 0;
assign temp7 = (temp6 >= 200000) ? temp6 - 200000 : temp6;
assign outc[0] = (temp7 >= 100000) ? 1 : 0;
assign temp8 = (temp7 >= 100000) ? temp7 - 100000 : temp7;
//万位计算d
assign outd[3] = (temp8 >= 80000) ? 1 : 0;
assign temp9 = (temp8 >= 80000) ? temp8 - 80000 : temp8;
assign outd[2] = (temp9 >= 40000) ? 1 : 0;
assign temp10 = (temp9 >= 40000) ? temp9 - 40000 : temp9;
assign outd[1] = (temp10 >= 20000) ? 1 : 0;
assign temp11 = (temp10 >= 20000) ? temp10 - 20000 : temp10;
assign outd[0] = (temp11 >= 10000) ? 1 : 0;
assign temp12 = (temp11 >= 10000) ? temp11 - 10000 : temp11;
// 千位计算e
assign oute[3] = (temp12 >= 8000) ? 1 : 0;
assign temp13 = (temp12 >= 8000) ? temp12 - 8000 : temp12;
assign oute[2] = (temp13 >= 4000) ? 1 : 0;
assign temp14 = (temp13 >= 4000) ? temp13 - 4000 : temp13;
assign oute[1] = (temp14 >= 2000) ? 1 : 0;
assign temp15 = (temp14 >= 2000) ? temp14 - 2000 : temp14;
assign oute[0] = (temp15 >= 1000) ? 1 : 0;
assign temp16 = (temp15 >= 1000) ? temp15 - 1000 : temp15;
// 百位计算f
assign outf[3] = (temp16 >= 800) ? 1 : 0;
assign temp17 = (temp16 >= 800) ? temp16 - 800 : temp16;
assign outf[2] = (temp17 >= 400) ? 1 : 0;
assign temp18 = (temp17 >= 400) ? temp17 - 400 : temp17;
assign outf[1] = (temp18 >= 200) ? 1 : 0;
assign temp19 = (temp18 >= 200) ? temp18 - 200 : temp18;
assign outf[0] = (temp19 >= 100) ? 1 : 0;
assign temp20 = (temp19 >= 100) ? temp19 - 100 : temp19;
// 十位计算g
assign outg[3] = (temp20 >= 80) ? 1 : 0;
assign temp21 = (temp20 >= 80) ? temp20 - 80 : temp20;
assign outg[2] = (temp21 >= 40) ? 1 : 0;
assign temp22 = (temp21 >= 40) ? temp21 - 40 : temp21;
assign outg[1] = (temp22 >= 20) ? 1 : 0;
assign temp23 = (temp22 >= 20) ? temp22 - 20 : temp22;
assign outg[0] = (temp23 >= 10) ? 1 : 0;
assign temp24 = (temp23 >= 10) ? temp23 - 10 : temp23;
// 个位计算h
assign outh = (temp24 >= 10) ? temp24 -10 : temp24;

endmodule

八段数码管显示模块ledout.v

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Ledout: Translation from BCD to LED_out
module ledout(in, out);
input [3:0] in;
output [7:0] out;
reg [7:0] out;

always @(in)
begin
case(in)
0: out = 8'b11111100;
1: out = 8'b01100000;
2: out = 8'b11011010;
3: out = 8'b11110010;
4: out = 8'b01100110;
5: out = 8'b10110110;
6: out = 8'b10111110;
7: out = 8'b11100000;
8: out = 8'b11111110;
9: out = 8'b11110110;
default: out = 8'bXXXXXXXX;
endcase
end
endmodule

跨时钟域同步模块(1bit按键交互)syncro.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
// Syncro: Asyncronous to Syncronous (1-bit width)
module syncro(out, in, clk, reset);
parameter WIDTH = 1;
input [WIDTH-1:0] in ;
output [WIDTH-1:0] out;
input clk,reset;
reg [WIDTH-1:0] qO,q1,q2;

always @(posedge clk or negedge reset)
begin
if(!reset)
begin
qO <= 0;
q1 <= 0;
q2 <= 0;
end
else
begin
qO <= ~in;
q1 <= qO;
q2 <= q1;
end
end
assign out=q1 & (~q2) ;
endmodule

跨时钟域同步模块(10bit按键交互)syncro10.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
// Syncro10: Asyncronous to Syncrnous (10-bit width)
module syncro10(out, in, clk, reset) ;
parameter WIDTH = 10;
input [WIDTH-1:0] in;
output [WIDTH-1:0] out;
input clk,reset;
reg [WIDTH-1:0] qO,q1,q2;

always @(posedge clk or negedge reset)
begin
if(!reset)
begin
qO <= 0;
q1 <= 0;
q2 <= 0;
end
else
begin
qO <= ~in;
q1 <= qO;
q2 <= q1;
end
end
assign out=q1 & (~q2) ;
endmodule

后记

虽然这个项目中我只是完成了较为简单的运算设计,创新点也较为简单(矩阵和井字棋),没有像有些同学写了小数和负数的运算。但是在上板验证阶段,我的代码一次性通过了所有的测试,准确无误;在课上进行presentation时思路也较为清晰,得到了三位学长助教的一致好评,最终也是拿到了30分满分的好成绩。