使用Quartus-II 9.1SP2 + ModelSim 6.5b-Aletra + Altera DE2-115 FPGA開發平台,設計 8-bit ALU part3 為例(FPGA開發平台)
3'b000: y = a + b; // op=000, 執行 a + b
3'b001: y = a + 1; // op=001, 執行 a + 1
3'b010: y = a - b; // op=010, 執行 a - b
3'b011: y = a ^ b; // op=011, 執行 a xor b
3'b100: y = a | b; // op=100, 執行 a or b
3'b101: y = a & b; // op=101, 執行 a and b
3'b110: y = ~a; // op=110, 執行 not a
3'b111: y = a<<1; // op=111, 執行 a << 1’b1
https://www.youtube.com/watch?v=tpkaOm_Su74
module DE2_115 (SW, LEDR, LEDG , CLOCK_50 ,KEY ,HEX0 ,HEX1 ,HEX2,HEX3,HEX4 ,HEX5 ,HEX6,HEX7, GPIO );
input [17:0] SW; // toggle switches
input [7:0] KEY; // Push bottom
input CLOCK_50; //Clock 27MHz , 50Mhz
output [17:0] LEDR; // red LEDS
output [8:0] LEDG; // green LEDs
output [6:0] HEX0,HEX1,HEX2,HEX3; //7-segment display
output [6:0] HEX4,HEX5,HEX6,HEX7; //7-segment display
inout [35:0] GPIO;
//assign HEX0=7'b111_1111;
//assign HEX1=7'b111_1111;
//assign HEX2=7'b111_1111;
//assign HEX3=7'b111_1111;
assign HEX4=7'b111_1111;
assign HEX5=7'b111_1111;
assign HEX6=7'b111_1111;
assign HEX7=7'b111_1111;
//reg8 (reset_n,EN, CLK, Din, Qout);
// input reset_n;
// input CLK;
// input EN,
// input [7:0] Din;
// output [7:0] Qout;
wire [15:0]reg_temp;
wire [7:0]alu_out;
wire [15:0]bcd_out;
assign LEDR=reg_temp;
assign LEDG[7:0]=alu_out;
wire [7:0] segout0; //HEX 0
wire [7:0] segout1; //HEX 1
wire [7:0] segout2; //HEX 2
wire [7:0] segout3; //HEX 3
input [7:0] KEY; // Push bottom
input CLOCK_50; //Clock 27MHz , 50Mhz
output [17:0] LEDR; // red LEDS
output [8:0] LEDG; // green LEDs
output [6:0] HEX0,HEX1,HEX2,HEX3; //7-segment display
output [6:0] HEX4,HEX5,HEX6,HEX7; //7-segment display
inout [35:0] GPIO;
//assign HEX0=7'b111_1111;
//assign HEX1=7'b111_1111;
//assign HEX2=7'b111_1111;
//assign HEX3=7'b111_1111;
assign HEX4=7'b111_1111;
assign HEX5=7'b111_1111;
assign HEX6=7'b111_1111;
assign HEX7=7'b111_1111;
//reg8 (reset_n,EN, CLK, Din, Qout);
// input reset_n;
// input CLK;
// input EN,
// input [7:0] Din;
// output [7:0] Qout;
wire [15:0]reg_temp;
wire [7:0]alu_out;
wire [15:0]bcd_out;
assign LEDR=reg_temp;
assign LEDG[7:0]=alu_out;
wire [7:0] segout0; //HEX 0
wire [7:0] segout1; //HEX 1
wire [7:0] segout2; //HEX 2
wire [7:0] segout3; //HEX 3
reg8 u0(KEY[0], SW[17],CLOCK_50,SW[7:0],reg_temp[7:0]);
reg8 u1(KEY[0], SW[16],CLOCK_50,SW[7:0],reg_temp[15:8]);
//alu(input [7:0] a, input [7:0] b, input [2:0] op, output reg [7:0] y);
alu u2(reg_temp[7:0],reg_temp[15:8],SW[15:13],alu_out);
alu u2(reg_temp[7:0],reg_temp[15:8],SW[15:13],alu_out);
//bin2bcd( bin, bcd);
// input [7:0] bin;
// output [15:0] bcd;
bin2bcd u3(alu_out,bcd_out);
_7seg UUT0(.hex((bcd_out[3:0])) , .seg(segout0));
_7seg UUT1(.hex((bcd_out[7:4])) , .seg(segout1));
_7seg UUT2(.hex((bcd_out[11:8])) , .seg(segout2));
_7seg UUT3(.hex((bcd_out[15:12])), .seg(segout3));
assign HEX0=segout0[6:0];
assign HEX1=segout1[6:0];
assign HEX2=segout2[6:0];
assign HEX3=segout3[6:0];
endmodule
module bin2bcd( bin, bcd);
//input ports and their sizes
input [7:0] bin;
//output ports and, their size
output [15:0] bcd;
//Internal variables
reg [15 : 0] bcd;
reg [3:0] i;
//Always block - implement the Double Dabble algorithm
always @(bin)
begin
bcd = 0; //initialize bcd to zero.
for (i = 0; i < 8; i = i+1) //run for 8 iterations
begin
bcd = {bcd[10:0],bin[7-i]}; //concatenation
//if a hex digit of 'bcd' is more than 4, add 3 to it.
if(i < 7 && bcd[3:0] > 4)
bcd[3:0] = bcd[3:0] + 3;
if(i < 7 && bcd[7:4] > 4)
bcd[7:4] = bcd[7:4] + 3;
if(i < 7 && bcd[11:8] > 4)
bcd[11:8] = bcd[11:8] + 3;
end
end
endmodule
//-----------------------------------------
//Common-cathod seven segment display
//using case.....endcase statement
//Filename : sevenseg_case.v
//-----------------------------------------
module _7seg(hex , seg);
input [3:0] hex;
output [7:0] seg;
reg [7:0] seg;
// segment encoding
// 0
// ---
// 5 | | 1
// --- <- 6
// 4 | | 2
// ---
// 3
always @(hex)
begin
case (hex)
// Dot point is always disable
4'b0001 : seg = 8'b11111001; //1 = F9H
4'b0010 : seg = 8'b10100100; //2 = A4H
4'b0011 : seg = 8'b10110000; //3 = B0H
4'b0100 : seg = 8'b10011001; //4 = 99H
4'b0101 : seg = 8'b10010010; //5 = 92H
4'b0110 : seg = 8'b10000010; //6 = 82H
4'b0111 : seg = 8'b11111000; //7 = F8H
4'b1000 : seg = 8'b10000000; //8 = 80H
4'b1001 : seg = 8'b10010000; //9 = 90H
4'b1010 : seg = 8'b10001000; //A = 88H
4'b1011 : seg = 8'b10000011; //b = 83H
4'b1100 : seg = 8'b11000110; //C = C6H
4'b1101 : seg = 8'b10100001; //d = A1H
4'b1110 : seg = 8'b10000110; //E = 86H
4'b1111 : seg = 8'b10001110; //F = 8EH
default : seg = 8'b11000000; //0 = C0H
endcase
end
endmodule
// 輸入 a, b 後會執行 op 所指定的運算,然後將結果放在暫存器 y 當中
module alu(input [7:0] a, input [7:0] b, input [2:0] op, output reg [7:0] y);
always@(a or b or op) begin // 當 a, b 或 op 有改變時,就進入此區塊執行。
case(op) // 根據 op 決定要執行何種運算
3'b000: y = a + b; // op=000, 執行 a + b
3'b001: y = a + 1; // op=001, 執行 a + 1
3'b010: y = a - b; // op=010, 執行 a - b
3'b011: y = a ^ b; // op=011, 執行 a xor b
3'b100: y = a | b; // op=100, 執行 a or b
3'b101: y = a & b; // op=101, 執行 a and b
3'b110: y = ~a; // op=110, 執行 not a
3'b111: y = a<<1; // op=111, 執行 a << 1’b1
endcase
end
endmodule
//ALU_8bit_REG
module reg8 (reset_n,EN, CLK, Din, Qout);
input reset_n;
input CLK;
input EN;
input [7:0] Din;
output [7:0] Qout;
reg [7:0] Qout;
always @(posedge CLK , negedge reset_n )
begin
if (!reset_n)
Qout = 0;
else if (EN)
Qout = Din;
end
endmodule // reg8
沒有留言:
張貼留言