BCD -> One-Hot
输出全置0代表无效输入
xxxxxxxxxx221module DCD4_10 (2 input [3:0] d, //4位BCD码3 output reg [9:0] y //10位译码,高电平有效4);5
6 always @ () begin7 case(d)8 4'b0000: y 10'b0000000001;9 4'b0001: y 10'b0000000010;10 4'b0010: y 10'b0000000100;11 4'b0011: y 10'b0000001000;12 4'b0100: y 10'b0000010000;13 4'b0101: y 10'b0000100000;14 4'b0110: y 10'b0001000000;15 4'b0111: y 10'b0010000000;16 4'b1000: y 10'b0100000000;17 4'b1001: y 10'b1000000000;18 default: y 10'b0000000000;19 endcase20 end21 22endmodulexxxxxxxxxx181module Decoder_3_8_n(2 input [2:0] in,3 output reg [7:0] out4 );5 always @ () begin6 case(in)7 3'b000: out 8'b11111110;8 3'b001: out 8'b11111101;9 3'b010: out 8'b11111011;10 3'b011: out 8'b11110111;11 3'b100: out 8'b11101111;12 3'b101: out 8'b11011111;13 3'b110: out 8'b10111111;14 3'b111: out 8'b01111111;15 default: out 8'b11111111;16 endcase17 end18endmodulexxxxxxxxxx361module encoder10_4 (2 input e,3 input [9:0] a,4 output reg f,5 output reg [3:0] y6);7
8 always @() begin9 f e;10 y 4'b0000;11 if (e) begin12 if (a[9]) begin13 y 4'b1001;14 end else if (a[8]) begin15 y 4'b1000;16 end else if (a[7]) begin17 y 4'b0111;18 end else if (a[6]) begin19 y 4'b0110;20 end else if (a[5]) begin21 y 4'b0101;22 end else if (a[4]) begin23 y 4'b0100;24 end else if (a[3]) begin25 y 4'b0011;26 end else if (a[2]) begin27 y 4'b0010;28 end else if (a[1]) begin29 y 4'b0001;30 end else if (a[0]) begin31 y 4'b0000;32 end33 end34 end35 36endmodule有 Structural Continual Behavioral 三个逻辑的数据选择器
xxxxxxxxxx881module mux2_1 (2 input a, b, 3 input sel,4 output f5 );6 wire nsel, f1, f2;7/// Structural8 and G1 (f1, a, nsel),9 G2 (f2, b, sel);10 or G3 (f, f1, f2);11 not G4 (nsel, sel);12 13/// Continual Assign14// assign f = (a & ~sel) | (b & sel);15// assign f = sel ? b : a;16
17/* Behavioral18 reg f;19 always @(*) begin20 if (sel == 0) f = a;21 else f = b;22 end23*/24endmodule25
26/////////////////////////////////////////////////////////////////27module mux2_2 (28 input [1:0] a, b, 29 input sel,30 output [1:0] f31 );32 mux2_1 M0 (a[0], b[0], sel, f[0]); 33 mux2_1 M1 (a[1], b[1], sel, f[1]); 34// assign f = sel ? b : a; 35endmodule36
37/////////////////////////////////////////////////////////////////38module mux4_1 (39 input a, b, c, d,40 input [1:0] s,41 output y 42 );43 wire l, h;44/// Structural45 mux2_1 M0 (a, b, s[0], l);46 mux2_1 M1 (c, d, s[0], h);47 mux2_1 M2 (l, h, s[1], y);48 49/* Behavioral50 reg y;51 always @* begin52 case (s)53 2'b00: y = a;54 2'b01: y = b;55 2'b10: y = c;56 default: y = d;57 endcase58 end59*/ 60endmodule61
62/////////////////////////////////////////////////////////////////63module mux2 # (parameter WIDTH 4) (64 input [WIDTH1:0] a, b, 65 input sel,66 output [WIDTH1:0] f67 );68 assign f sel b : a; 69endmodule70
71/////////////////////////////////////////////////////////////////72module mux4_8 (73 input [7:0] a, b, c, d,74 input [1:0] s,75 output [7:0] y 76 );77 wire [7:0] l, h;78
79 mux2 M0 (a[3:0], b[3:0], s[0], l[3:0]);80 mux2 M1 (a[7:4], b[7:4], s[0], l[7:4]);81// mux2 #8 M01 (a, b, s[0], l); 82 83 mux2 #8 M2 (c, d, s[0], h);84 85// mux2 #8 M3 (l, h, s[1], y); //按顺序传递86 mux2 #8 M3 (.f(y), .sel(s[1]), .a(l), .b(h)); //按名字传递87endmodule88
xxxxxxxxxx111module MUX_2 #(parameter WIDTH 322 )(3 input [WIDTH1:0] in0, in1,4 input sel,5 output reg [WIDTH1:0] out6 );7 always @() begin8 if (sel) out < in1;9 else out < in0;10 end11endmodulexxxxxxxxxx281module seven_disp_decoder (2 input [3:0] d, //4位BCD码3 output reg [6:0] yn //7段字形,a, b, … g依次对应y[6], y[5], … y[0], 低电平有效 4);5
6always @() begin7 case (d)8 4'b0000: yn 7'b0000001; //09 4'b0001: yn 7'b1001111; //110 4'b0010: yn 7'b0010010; //211 4'b0011: yn 7'b0000110; //312 4'b0100: yn 7'b1001100; //413 4'b0101: yn 7'b0100100; //514 4'b0110: yn 7'b0100000; //615 4'b0111: yn 7'b0001111; //716 4'b1000: yn 7'b0000000; //817 4'b1001: yn 7'b0000100; //918 4'b1010: yn 7'b0001000; //A19 4'b1011: yn 7'b1100000; //B20 4'b1100: yn 7'b0110001; //C21 4'b1101: yn 7'b1000010; //D22 4'b1110: yn 7'b0110000; //E23 4'b1111: yn 7'b0111000; //F24 default: yn 7'b1111111; //invalid25 26 endcase27end28endmodulexxxxxxxxxx11221`timescale 1ns 1ps2//////////////////////////////////////////////////////////////////////////////////3// Company: 4// Engineer: 5// 6// Create Date: 2022/11/01 23:15:167// Design Name: 8// Module Name: Display9// Project Name: 10// Target Devices: 11// Tool Versions: 12// Description: 13// 14// Dependencies: 15// 16// Revision:17// Revision 0.01 - File Created18// Additional Comments:19// 20//////////////////////////////////////////////////////////////////////////////////21
22
23module Display(24 input clk,25 input [31:0] d,26 output reg [6:0] cn,27 output reg [7:0] an28 // ,output reg [2:0] a_o,29 // output Clk_new_out,30 // output reg [3:0] D31 );32 parameter [6:0]33 S_0 7'b1000000,34 S_1 7'b1111001,35 S_2 7'b0100100,36 S_3 7'b0110000,37 S_4 7'b0011001,38 S_5 7'b0010010,39 S_6 7'b0000010,40 S_7 7'b1111000,41 S_8 7'b0000000,42 S_9 7'b0010000,43 S_A 7'b0001000,44 S_B 7'b0000011,45 S_C 7'b1000110,46 S_D 7'b0100001,47 S_E 7'b0000110,48 S_F 7'b0001110;49
50 wire Clk_new;51 //assign Clk_new_out = Clk_new;52 wire [2:0] a;53 wire [7:0] a_out;54 reg rstn 0;55
56 Frequency_Devider #(32) FQD(57 .clk(clk),58 .k(10000),59 .y(Clk_new)60 );61
62 Counter #(.WIDTH(3), .RST_VLU(0)) CTR(63 .clk(Clk_new),64 .rstn(rstn),65 .pe(1'b0),66 .ce(1'b1),67 .d(0),68 .q(a)69 );70
71 Decoder_3_8_n DCDN(72 .in(a),73 .out(a_out)74 );75
76 reg [3:0] temp;77
78 always @(a,d) begin79 case(a)80 3'b000: temp d[3:0];81 3'b001: temp d[7:4];82 3'b010: temp d[11:8];83 3'b011: temp d[15:12];84 3'b100: temp d[19:16];85 3'b101: temp d[23:20];86 3'b110: temp d[27:24];87 3'b111: temp d[31:28];88 endcase89 end90
91
92 always @(posedge clk) begin93 an < a_out;94 // a_o <= a;95 // D <= temp;96
97 if (rstn) begin98 rstn<1'b1;99 cn< 7'b1111111;100 end101 case(temp)102 4'b0000: cn < S_0;103 4'b0001: cn < S_1;104 4'b0010: cn < S_2;105 4'b0011: cn < S_3;106 4'b0100: cn < S_4;107 4'b0101: cn < S_5;108 4'b0110: cn < S_6;109 4'b0111: cn < S_7;110 4'b1000: cn < S_8;111 4'b1001: cn < S_9;112 4'b1010: cn < S_A;113 4'b1011: cn < S_B;114 4'b1100: cn < S_C;115 4'b1101: cn < S_D;116 4'b1110: cn < S_E;117 4'b1111: cn < S_F;118 endcase119 end120 121endmodule122
模块后紧跟一个括号,在里面依次填入参数(parameter)默认为8
xxxxxxxxxx141module Register2 #(parameter WIDTH 8)3 (4 input Clk, rstn, en,5 input [WIDTH1:0] D,6 output reg [WIDTH1:0] Q7 );8 always @(posedge Clk, negedge rstn) begin9 if(rstn) 10 Q < 0;11 else if(en) 12 Q < D;13 end14endmodule
xxxxxxxxxx211module Carry_forward_Adder_2D(2 input [1:0] A,3 input [1:0] B,4 input Ci,5 output [1:0] S,6 output Co7 );8 wire [1:1]C;9 wire [1:0]G, P;10 assign C[1] Ci;11 assign G A[1:0] B[1:0];12 assign P A[1:0] B[1:0];13
14 assign C[0] G[0] (P[0] C[1]);15 assign C[1] G[1] P[1] (G[0] (P[0] C[1]));16
17 assign S P[1:0] C[0:1];18 assign Co C[1];19 20
21endmodulexxxxxxxxxx241module Carry_forward_Adder_4D(//四位超前进位加法器2 input [3:0] A,3 input [3:0] B,4 input Ci,5 output [3:0] S,6 output Co7 );8 wire [3:1]C;9 wire [3:0]G, P;10 assign C[1] Ci;11
12 assign G A[3:0] B[3:0];13 assign P A[3:0] B[3:0];14
15 assign C[0] G[0] (P[0] C[1]);16 assign C[1] G[1] P[1] (G[0] (P[0] C[1]));17 assign C[2] G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1])));18 assign C[3] G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1]))));19 20 assign Co C[3];21
22 assign S[3:0] P[3:0] C[2:1];23
24endmodulexxxxxxxxxx271module Carry_forward_Adder_8D(2 input [7:0] A,3 input [7:0] B,4 input Ci,5 output [7:0] S,6 output Co7 );8 wire [7:1]C;9 wire [7:0]G, P;10 assign C[1] Ci;11
12 assign G A B;13 assign P A B;14
15 assign C[0] G[0] (P[0] C[1]);16 assign C[1] G[1] P[1] (G[0] (P[0] C[1]));17 assign C[2] G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1])));18 assign C[3] G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1]))));19 assign C[4] G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1])))));20 assign C[5] G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1]))))));21 assign C[6] G[6] P[6] (G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1])))))));22 assign C[7] G[7] P[7] (G[6] P[6] (G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1]))))))));23 24 assign S P[7:0] C[6:1];25 assign Co C[7];26 27endmodulexxxxxxxxxx341module Carry_forward_Adder_16D(2 input [15:0] A,3 input [15:0] B,4 input Ci,5 output [15:0] S,6 output Co7 );8 wire [15:1]C;9 wire [15:0]G, P;10 assign C[1] Ci;11 assign G A[15:0] B[15:0];12 assign P A[15:0] B[15:0];13
14 assign C[0] G[0] (P[0] C[1]);15 assign C[1] G[1] P[1] (G[0] (P[0] C[1]));16 assign C[2] G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1])));17 assign C[3] G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1]))));18 assign C[4] G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1])))));19 assign C[5] G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1]))))));20 assign C[6] G[6] P[6] (G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1])))))));21 assign C[7] G[7] P[7] (G[6] P[6] (G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1]))))))));22 assign C[8] G[8] P[8] (G[7] P[7] (G[6] P[6] (G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1])))))))));23 assign C[9] G[9] P[9] (G[8] P[8] (G[7] P[7] (G[6] P[6] (G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1]))))))))));24 assign C[10] G[10] P[10] (G[9] P[9] (G[8] P[8] (G[7] P[7] (G[6] P[6] (G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1])))))))))));25 assign C[11] G[11] P[11] (G[10] P[10] (G[9] P[9] (G[8] P[8] (G[7] P[7] (G[6] P[6] (G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1]))))))))))));26 assign C[12] G[12] P[12] (G[11] P[11] (G[10] P[10] (G[9] P[9] (G[8] P[8] (G[7] P[7] (G[6] P[6] (G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1])))))))))))));27 assign C[13] G[13] P[13] (G[12] P[12] (G[11] P[11] (G[10] P[10] (G[9] P[9] (G[8] P[8] (G[7] P[7] (G[6] P[6] (G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1]))))))))))))));28 assign C[14] G[14] P[14] (G[13] P[13] (G[12] P[12] (G[11] P[11] (G[10] P[10] (G[9] P[9] (G[8] P[8] (G[7] P[7] (G[6] P[6] (G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1])))))))))))))));29 assign C[15] G[15] P[15] (G[14] P[14] (G[13] P[13] (G[12] P[12] (G[11] P[11] (G[10] P[10] (G[9] P[9] (G[8] P[8] (G[7] P[7] (G[6] P[6] (G[5] P[5] (G[4] P[4] (G[3] P[3] (G[2] P[2] (G[1] P[1] (G[0] (P[0] C[1]))))))))))))))));30
31 assign S P[15:0] C[14:1];32 assign Co C[15];33
34endmodulexxxxxxxxxx431module Comparer2 #(parameter WIDTH 8)3 (4 input [WIDTH:0] a,5 input [WIDTH:0] b,6 output reg ug, ul, sg, sl7 );8 wire [WIDTH:0] a_b_minus;9
10 assign a_b_minus a b;11 always @() begin12 if(a[WIDTH] b[WIDTH]) begin13 ug 1;14 ul 0;15 sg 0;16 sl 1;17 end 18 else if(a[WIDTH] b[WIDTH]) begin19 ug 0;20 ul 1;21 sg 1;22 sl 0;23 end 24 else begin25 if (a_b_minus) begin26 {ug, ul, sg, sl} 4'b0;27 end28 else if (a_b_minus[WIDTH]) begin29 ug 0;30 ul 1;31 sg 0;32 sl 1;33 end 34 else begin35 ug 1;36 ul 0;37 sg 1;38 sl 0;39 end40 end41 end42 43endmodulexxxxxxxxxx101module Comparer_2(2 input [1:0] a,3 input [1:0] b,4 output ug, ul, sg, sl5);6 assign ug a[1] b[1] (a[1] b[1]) a[0] b[0];7 assign ul a[1] b[1] (a[1] b[1]) a[0] b[0];8 assign sg a[1] b[1] (a[1] b[1]) a[0] b[0] (a[1] b[1]) a[0] b[0];9 assign sl a[1] b[1] (a[1] b[1]) a[0] b[0] (a[1] b[1]) a[0] b[0];10endmodulexxxxxxxxxx301module Comparer_4(2 input [3:0] a,3 input [3:0] b,4 output ug, ul, sg, sl5);6 wire ug_1, ul_1;7 wire ug_2, ul_2, sg_2, sl_2;8 Comparer_2 com1(9 .a(a[1:0]),10 .b(b[1:0]),11 .ug(ug_1),12 .ul(ul_1),13 .sg(),14 .sl()15 );16 Comparer_2 com2(17 .a(a[3:2]),18 .b(b[3:2]),19 .ug(ug_2),20 .ul(ul_2),21 .sg(sg_2),22 .sl(sl_2)23 );24
25 assign ug ug_2 ul_2 ug_1;26 assign ul ul_2 ug_2 ul_1;27 assign sg sg_2 sl_2 ug_1;28 assign sl sl_2 sg_2 ul_1;29 30endmodulexxxxxxxxxx291module Comparer_8(2 input [7:0] a,3 input [7:0] b,4 output ug, ul, sg, sl5 );6 wire ug_1, ul_1;7 wire ug_2, ul_2, sg_2, sl_2;8 Comparer_4 com1(9 .a(a[3:0]),10 .b(b[3:0]),11 .ug(ug_1),12 .ul(ul_1),13 .sg(),14 .sl()15 );16 Comparer_4 com2(17 .a(a[7:4]),18 .b(b[7:4]),19 .ug(ug_2),20 .ul(ul_2),21 .sg(sg_2),22 .sl(sl_2)23 );24
25 assign ug ug_2 ul_2 ug_1;26 assign ul ul_2 ug_2 ul_1;27 assign sg sg_2 sl_2 ug_1;28 assign sl sl_2 sg_2 ul_1;29endmodulexxxxxxxxxx91module Multiply_2(2 input [1:0] A, B,3 output [3:0] P4 );5 assign P[0] A[0] B[0];6 assign P[1] (A[0] B[1]) (A[1] B[0]);7 assign P[2] A[1] B[1] (A[0] B[0]);8 assign P[3] (A) (B);9endmodulexxxxxxxxxx521module Multiply_4(2 input [3:0] A, B,3 output [7:0] P4 );5 wire [3:0] PM1, PM2, PM3, PM4;6 wire [7:0] PP1, PP2, PP3;7 Multiply_2 M1(8 .A(A[1:0]),9 .B(B[1:0]),10 .P(PM1)11 );12
13 Multiply_2 M2(14 .A(A[3:2]),15 .B(B[1:0]),16 .P(PM2)17 );18
19 Multiply_2 M3(20 .A(A[1:0]),21 .B(B[3:2]),22 .P(PM3)23 );24
25 Multiply_2 M4(26 .A(A[3:2]),27 .B(B[3:2]),28 .P(PM4)29 );30
31 assign PP1 {PM4, PM1};32 Carry_forward_Adder_4D ADD1(33 .A(PM2),34 .B(PM3),35 .Ci(0),36 .S(PP2[5:2]),37 .Co(PP2[6])38 );39 assign PP2[7] 0;40 assign PP2[1:0] 0;41 Carry_forward_Adder_8D ADD2(42 .A(PP1),43 .B(PP2),44 .Ci(0),45 .S(PP3),46 .Co()47 );48
49 assign P PP3;50
51
52endmodulexxxxxxxxxx501module Multiply_8(2 input [7:0] A, B,3 output [15:0] P4);5 wire [7:0] PM1, PM2, PM3, PM4;6 wire [15:0] PP1, PP2, PP3;7 Multiply_4 M1(8 .A(A[3:0]),9 .B(B[3:0]),10 .P(PM1)11 );12
13 Multiply_4 M2(14 .A(A[7:4]),15 .B(B[3:0]),16 .P(PM2)17 );18
19 Multiply_4 M3(20 .A(A[3:0]),21 .B(B[7:4]),22 .P(PM3)23 );24
25 Multiply_4 M4(26 .A(A[7:4]),27 .B(B[7:4]),28 .P(PM4)29 );30
31 assign PP1 {PM4, PM1};32 Carry_forward_Adder_8D ADD1(33 .A(PM2),34 .B(PM3),35 .Ci(0),36 .S(PP2[11:4]),37 .Co(PP2[12])38 );39 assign PP2[15:13] 0;40 assign PP2[3:0] 0;41 Carry_forward_Adder_16D ADD2(42 .A(PP1),43 .B(PP2),44 .Ci(0),45 .S(PP3),46 .Co()47 );48
49 assign P PP3;50endmodule可自定义复位
xxxxxxxxxx141module Counter #(2 parameter WIDTH 16, 3 RST_VLU 04)(5 input clk, rstn, pe, ce, 6 input [WIDTH1:0] d,7 output reg [WIDTH1:0] q8);9 always @(posedge clk, negedge rstn) begin10 if (rstn) q < RST_VLU;11 else if (pe) q < d;12 else if (ce) q < q 1; 13 end14endmodule可自定义长度(这边建议总线输入输出)
可自定义去抖动时长(可以调小点进行)
xxxxxxxxxx311module Debounce #(parameter WIDTH 1, parameter TIME_HOLD 1000000)(2 input clk, rstn, [WIDTH1:0] in,3 output reg [WIDTH1:0] out4 //,output reg [31:0] counter5 );6
7 reg [31:0] count TIME_HOLD;8 reg [WIDTH1:0] state;9
10 always @(posedge clk, negedge rstn) begin11 //counter = count;12 if (rstn) begin13 count < TIME_HOLD;14 out < 0;15 state < in;16 end17 else if (in state) begin18 if (count 0) begin19 out < state;20 end21 else begin22 count < count 1;23 end24 end25 else begin26 count < TIME_HOLD;27 state < in;28 out < 0;29 end30 end31endmoduleWIDTH宽 变为原来的1/k
xxxxxxxxxx291module Frequency_Devider#(2 parameter WIDTH 323 )( 4 input clk,5 input [WIDTH1:0] k,6 output reg y7 );8
9 reg [WIDTH1:0] count;10
11 initial begin 12 count k 1;13 y 0;14 end15
16 always @ (posedge clk) begin17 if (count 0) begin18 count < k 1;19 y < y;20 end21 else if (count (k)2) begin22 y < y;23 count < count 1;24 end25 else begin26 count < count 1;27 end28 end29endmodulexxxxxxxxxx141module Posedge_Selector(2 input clk, rstn, in, st_pos,3 output reg out4 );5 reg last;6 always @(posedge clk, negedge rstn) begin7 if (st_pos) last < 1;8 if (rstn) out < 0;9 else begin10 out < in last;11 last < in;12 end13 end14endmodulexxxxxxxxxx261module Register_File # (2 parameter AW 5, //地址宽度3 parameter DW 16 //数据宽度4)(5 input clk, //时钟6 input [AW1:0] ra0, ra1, //读地址7 output [DW1:0] rd0, rd1, //读数据8 input [AW1:0] wa1, wa0, //写地址9 input [DW1:0] wd1, wd0, //写数据10 input we1, we0 //写使能11);12 reg [DW1:0] rf [0: (1<<AW)1]; //寄存器堆13 integer i0;14 initial begin15 for (i0 0; i0 < 2AW; i0 i0 1) begin16 rf[i0] i0;17 end18 end19 //integer i;20 //initial for (i=0; i<(1<<AW); i=i+1) rf[i] = 0; //初始化寄存器堆21 assign rd0 rf[ra0], rd1 rf[ra1]; //读操作22 always @ (posedge clk) begin23 if (we0) rf[wa0] < wd0;24 if (we1) rf[wa1] < wd1; //写操作25 end26endmoduleDDP 是显示数据处理
DST 是扫描时间产生
xxxxxxxxxx251module Display_Unit(2 input clk, rstn,3 input [11:0] rdata,4 input [14:0] waddr,5 input [11:0] wdata,6 output hs, vs,7 output [11:0] rgb,8 output [14:0] raddr9 );10
11 wire hen, ven;12
13 Display_Data_Processing DDP(14 .hen(hen), .ven(ven), .clk(clk), .rstn(rstn),15 .waddr(waddr), .wdata(wdata),16 .rdata(rdata), .raddr(raddr), .rgb(rgb)17 );18
19 Disply_Scan_Timing DST(20 .clk(clk), .rstn(rstn),21 .hs(hs), .vs(vs),22 .hen(hen), .ven(ven)23 );24
25endmodule下面是一组DDP和DST
xxxxxxxxxx941module Display_Data_Processing(2 input hen, ven, clk, rstn,3 input [11:0] wdata,4 output [14:0] waddr,5 input [11:0] rdata,6 output [14:0] raddr,7 output reg [11:0] rgb8 );9
10 reg [14:0] x 0, y 0;11 assign raddr x[14:2] (y[14:2]<<7) (y[14:2] << 6) (y[14:2] << 3);12 reg [23:0] count 0;13 always @(posedge clk or negedge rstn)begin14 if(rstn)begin15 x < 0;16 y < 0;17 rgb < 0;18 count < 0;19 end20 else if(hen ven)begin21 count < count 1;22 if (x 799)begin23 x < 0;24 if (y 599)25 y < 0;26 else y < y 1;27 end28 else x < x 1;29 if(count[23] ((waddr raddr 1) waddr raddr 1 waddr raddr 200 waddr raddr 200))begin30 rgb < wdata;31 end32 else begin33 rgb < rdata;34 end35 end36 else rgb < 0;37 end38endmodule39
40module Disply_Scan_Timing(41 input clk, rstn,42 output hen, ven, hs, vs43 );44 //wire clk_50mhz;45
46 reg [11:0] h_count 0, v_count 0;47
48 parameter [10:0]49 H_NEGATIVE 120,50 H_ACTIVE 184,51 H_INACTIVE 984,52 H_END 1039,53 V_NEGATIVE 6,54 V_ACTIVE 29,55 V_INACTIVE 629,56 V_END 665;57 //H_NEGATIVE = 1,58 //H_ACTIVE = 2,59 //H_INACTIVE = 10,60 //H_END = 11,61 //V_NEGATIVE = 1,62 //V_ACTIVE = 2,63 //V_INACTIVE = 3,64 //V_END = 4;65 always @(posedge clk or negedge rstn) begin66 if (rstn) begin67 h_count < 0;68 v_count < 0;69 end70 else begin71 if (h_count H_END) begin72 h_count < 0;73 if (v_count V_END) begin74 v_count < 0;75 end76 else begin77 v_count < v_count 1;78 end79 end80 else begin81 h_count < h_count 1;82 end83 end84 end85
86//同步87assign hs (h_count < H_NEGATIVE) 1 : 0;88assign vs (v_count < V_NEGATIVE) 1 : 0;89
90//使能91assign hen ((h_count > H_ACTIVE) (h_count < H_INACTIVE)) 1 : 0;92assign ven ((v_count > V_ACTIVE) (v_count < V_INACTIVE)) 1 : 0;93
94endmodule