**这是本文档旧的修订版!**
简单的BintoBCD模组
8位Binary转化为12位BCD
原理图
////////////////////////////////////////////////////////////////////////////////// // Company: Cal Poly Pomona // Engineer: Yongyuan Zhang // Lab 3 part A, question 1 // Create Date: 2018/05/04 10:40:12 // Description: Code of add 3 functions ////////////////////////////////////////////////////////////////////////////////// `timescale 1ns / 1ps module add3( input [3:0]x, output reg [3:0]y ); always@(*) begin if(x < 5) y = x; else y = x +3; // y = x ^4'b0111; end endmodule
8位Bin转化为BCD模组
////////////////////////////////////////////////////////////////////////////////// // Company: Cal Poly Pomona // Engineer: Yongyuan Zhang // Lab 3 part A, question 2 // Create Date: 2018/05/04 10:50:14 // Description: Code of binary to bcd ////////////////////////////////////////////////////////////////////////////////// `timescale 1ns / 1ps module bin2bcd( input[7:0] BIN, output [11:0] BCD ); wire [3:0] u0,u1,u2,u3,u5; add3 U0(.x( {1'b0,BIN[7:5]} ),.y(u0)); add3 U1(.x( {u0[2:0],BIN[4]} ),.y(u1)); add3 U2(.x( {u1[2:0],BIN[3]} ),.y(u2)); add3 U3(.x( {u2[2:0],BIN[2]} ),.y(u3)); add3 U4(.x( {u3[2:0],BIN[1]} ),.y(BCD[4:1])); add3 U5(.x( {1'b0,u0[3],u1[3],u2[3]} ), .y(u5)); add3 U6(.x( {u5[2:0],u3[3]}), . y(BCD[8:5])); assign BCD[0] = BIN[0]; assign BCD[11:9] = {2'b00,u5[3]}; endmodule
模组TestBench
////////////////////////////////////////////////////////////////////////////////// // Company: Cal Poly Pomona // Engineer: Yongyuan Zhang // Lab 3 part A, question 3 // Create Date: 2018/05/04 10:50:14 // Description: testbench for all possible conditions of the bin to bcd code ////////////////////////////////////////////////////////////////////////////////// module tb(); reg [7:0] BIN; wire [11:0] BCD; bin2bcd uut(.BIN(BIN),.BCD(BCD)); initial begin #2 BIN = 8'b0000_0000;//0 #2 BIN = 8'b0000_1010;//10 #2 BIN = 8'b0001_0100;//20 #2 BIN = 8'b0010_1000;//40 #2 BIN = 8'b0101_0000;//80 #2 BIN = 8'b1010_0000;//160 #2 BIN = 8'b0101_0010;//82 #2 BIN = 8'b1111_1111;//255 #2 BIN = 8'b0111_1010;//122 #2 BIN = 8'b1000_1100;//140 #2 BIN = 8'b1010_0011;//163 #2 BIN = 8'b1110_1100;//236 #2 BIN = 8'b1010_1111; //175 #2 $finish; end endmodule
最炫民族风case
always @(posedge clkout or negedge rst_n_in) begin if(!rst_n_in) begin tone <= 5'd6; timecnt <= 0; end else begin if(timecnt>=207) timecnt<=0; else begin timecnt <= timecnt + 1'b1; case(timecnt) //苍茫的天涯是 0: tone <= 5'd6; 1: tone <= 5'd6; 2: tone <= 5'd6; 3: tone <= 5'd5; 4: tone <= 5'd6; 5: tone <= 5'd6; 6: tone <= 5'd6; 7: tone <= 5'd8; //我的爱哎哎哎~ 8: tone <= 5'd8; 9: tone <= 5'd8; 10: tone <= 5'd9; 11: tone <= 5'd8; 12: tone <= 5'd6; 13: tone <= 5'd6; 14: tone <= 5'd6; 15: tone <= 5'd6; //绵绵的青山脚下 16: tone <= 5'd8; 17: tone <= 5'd8; 18: tone <= 5'd8; 19: tone <= 5'd5; 20: tone <= 5'd8; 21: tone <= 5'd9; 22: tone <= 5'd10; 23: tone <= 5'd12; //花正开 24: tone <= 5'd12; 25: tone <= 5'd10; 26: tone <= 5'd9; 27: tone <= 5'd9; 28: tone <= 5'd10; 29: tone <= 5'd10; 30: tone <= 5'd10; 31: tone <= 5'd10; //什么样的节奏是 32: tone <= 5'd13; 33: tone <= 5'd13; 34: tone <= 5'd13; 35: tone <= 5'd12; 36: tone <= 5'd10; 37: tone <= 5'd10; 38: tone <= 5'd10; 39: tone <= 5'd8; //最呀最摇摆 嘿! 40: tone <= 5'd6; 41: tone <= 5'd6; 42: tone <= 5'd6; 43: tone <= 5'd10; 44: tone <= 5'd9; 45: tone <= 5'd9; 46: tone <= 5'd9; 47: tone <= 5'd9; //什么样的歌声才是 48: tone <= 5'd10; 49: tone <= 5'd10; 50: tone <= 5'd12; 51: tone <= 5'd10; 52: tone <= 5'd9; 53: tone <= 5'd10; 54: tone <= 5'd9; 55: tone <= 5'd8; //最开怀~ 56: tone <= 5'd6; 57: tone <= 5'd6; 58: tone <= 5'd5; 59: tone <= 5'd5; 60: tone <= 5'd6; 61: tone <= 5'd6; 62: tone <= 5'd6; 63: tone <= 5'd6; //副歌 //我们要唱就要唱的最痛快 64: tone <= 5'd10; 65: tone <= 5'd10; 66: tone <= 5'd12; 67: tone <= 5'd10; 68: tone <= 5'd10; 69: tone <= 5'd12; 70: tone <= 5'd12; 71: tone <= 5'd13; 72: tone <= 5'd15; 73: tone <= 5'd13; 74: tone <= 5'd12; 75: tone <= 5'd12; 76: tone <= 5'd13; 77: tone <= 5'd13; 78: tone <= 5'd13; 79: tone <= 5'd13; //你是我天边 80: tone <= 5'd6; 81: tone <= 5'd6; 82: tone <= 5'd6; 83: tone <= 5'd5; 84: tone <= 5'd6; 85: tone <= 5'd6; 86: tone <= 5'd8; 87: tone <= 5'd8; //最美的云彩 88: tone <= 5'd9; 89: tone <= 5'd9; 90: tone <= 5'd8; 91: tone <= 5'd9; 92: tone <= 5'd10; 93: tone <= 5'd10; 94: tone <= 5'd10; 95: tone <= 5'd10; //让我用心把你留下 96: tone <= 5'd6; 97: tone <= 5'd13; 98: tone <= 5'd13; 99: tone <= 5'd12; 100: tone <= 5'd9; 101: tone <= 5'd9; 102: tone <= 5'd8; 103: tone <= 5'd9; //来,留下来! 104: tone <= 5'd10; 105: tone <= 5'd10; 106: tone <= 5'd10; 107: tone <= 5'd10; 108: tone <= 5'd10; 109: tone <= 5'd10; 110: tone <= 5'd10; 111: tone <= 5'd10; //悠悠的唱着最 112: tone <= 5'd8; 113: tone <= 5'd8; 114: tone <= 5'd6; 115: tone <= 5'd8; 116: tone <= 5'd9; 117: tone <= 5'd9; 118: tone <= 5'd5; 119: tone <= 5'd5; //炫的民族风 120: tone <= 5'd10; 121: tone <= 5'd12; 122: tone <= 5'd10; 123: tone <= 5'd9; 124: tone <= 5'd8; 125: tone <= 5'd8; 126: tone <= 5'd8; 127: tone <= 5'd8; //让爱卷走所有的尘 128: tone <= 5'd6; 129: tone <= 5'd8; 130: tone <= 5'd9; 131: tone <= 5'd10; 132: tone <= 5'd9; 133: tone <= 5'd8; 134: tone <= 5'd5; 135: tone <= 5'd3; //埃, 136: tone <= 5'd6; 137: tone <= 5'd6; 138: tone <= 5'd6; 139: tone <= 5'd6; 140: tone <= 5'd6; 141: tone <= 5'd6; 142: tone <= 5'd6; 143: tone <= 5'd6; //你是我心中 144: tone <= 5'd6; 145: tone <= 5'd6; 146: tone <= 5'd6; 147: tone <= 5'd5; 148: tone <= 5'd6; 149: tone <= 5'd6; 150: tone <= 5'd8; 151: tone <= 5'd8; //最美的云彩 152: tone <= 5'd9; 153: tone <= 5'd9; 154: tone <= 5'd8; 155: tone <= 5'd9; 156: tone <= 5'd10; 157: tone <= 5'd10; 158: tone <= 5'd10; 159: tone <= 5'd10; //斟满美酒让你留下 160: tone <= 5'd6; 161: tone <= 5'd13; 162: tone <= 5'd13; 162: tone <= 5'd12; 164: tone <= 5'd9; 165: tone <= 5'd9; 166: tone <= 5'd8; 167: tone <= 5'd9; //来,留下来! 168: tone <= 5'd10; 169: tone <= 5'd10; 170: tone <= 5'd10; 171: tone <= 5'd10; 172: tone <= 5'd10; 173: tone <= 5'd10; 174: tone <= 5'd10; 175: tone <= 5'd10; //永远都唱着最 176: tone <= 5'd8; 177: tone <= 5'd8; 178: tone <= 5'd6; 179: tone <= 5'd8; 180: tone <= 5'd9; 181: tone <= 5'd9; 182: tone <= 5'd5; 183: tone <= 5'd5; //炫的民族风,是 184: tone <= 5'd10; 185: tone <= 5'd12; 186: tone <= 5'd10; 187: tone <= 5'd9; 188: tone <= 5'd8; 189: tone <= 5'd8; 190: tone <= 5'd8; 191: tone <= 5'd8; //整片天空最美的姿 192: tone <= 5'd6; 193: tone <= 5'd8; 194: tone <= 5'd9; 195: tone <= 5'd10; 196: tone <= 5'd12; 197: tone <= 5'd10; 198: tone <= 5'd10; 199: tone <= 5'd12; //态!留下来! 200: tone <= 5'd13; 201: tone <= 5'd13; 202: tone <= 5'd13; 203: tone <= 5'd13; 204: tone <= 5'd13; 205: tone <= 5'd13; 206: tone <= 5'd13; 207: tone <= 5'd13; default: tone <= 5'd6; endcase end end end