使用Quartus-II 9.1SP2 + ModelSim 6.5b-Aletra + Altera DE2-115 FPGA開發平台,設計 Bidirectional Shift Register 為例(FPGA開發平台)
參考來源https://www.javatpoint.com/verilog-bidirectional-shift-register
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;
wire clk1;
//module Clock_1Hz(clk, reset, clk_1Hz);
//input clk, reset;
//output clk_1Hz;
Clock_1Hz(CLOCK_50,KEY[2],clk1);
assign LEDR[17]=clk1;
//module Bi_shift_reg #(parameter MSB=16) (
// input d,
// input clk,
// input en,
// input dir,
// input rstn,
// output reg [MSB-1:0] out);
Bi_shift_reg(SW[0],clk1,SW[1],SW[2],KEY[0],LEDR[15:0]);
endmodule
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;
wire clk1;
//module Clock_1Hz(clk, reset, clk_1Hz);
//input clk, reset;
//output clk_1Hz;
Clock_1Hz(CLOCK_50,KEY[2],clk1);
assign LEDR[17]=clk1;
//module Bi_shift_reg #(parameter MSB=16) (
// input d,
// input clk,
// input en,
// input dir,
// input rstn,
// output reg [MSB-1:0] out);
Bi_shift_reg(SW[0],clk1,SW[1],SW[2],KEY[0],LEDR[15:0]);
endmodule
module Bi_shift_reg #(parameter MSB=16) (
input d,
// Declare input for data to the first flop in the shift register
input clk,
// Declare input for the clock to all flops in the shift register
input en,
// Declare input for enable to switch the shift register on/off
input dir,
// Declare input to shift in either left or right direction
input rstn,
// Declare input to reset the register to a default value
output reg [MSB-1:0] out);
// Declare output to read out the current value of all flops in this register
// This always block will "always" be triggered on the rising edge of the clock
// Once it enters the block, it will first check to see if reset is 0 and if yes,
//then reset register
// If no, then check to see if the shift register is enabled
// If no => maintain previous output. If yes, then shift based on the requested direction
always @ (posedge clk)
if (!rstn)
out <= 0;
else begin
if (en)
case (dir)
0 : out <= {out[MSB-2:0], d};
1 : out <= {d, out[MSB-1:1]};
endcase
else
out <= out;
end
endmodule
//1 HZ CLOCK GENERATOR
module Clock_1Hz(clk, reset, clk_1Hz);
input clk, reset;
output clk_1Hz;
reg clk_1Hz = 1'b0;
reg [27:0] counter;
always@(negedge reset or posedge clk)
begin
if (!reset)
begin
clk_1Hz <= 0;
counter <= 0;
end
else
begin
counter <= counter + 1;
if ( counter == 25_000_000)
begin
counter <= 0;
clk_1Hz <= ~clk_1Hz;
end
end
end
endmodule
module Clock_1Hz(clk, reset, clk_1Hz);
input clk, reset;
output clk_1Hz;
reg clk_1Hz = 1'b0;
reg [27:0] counter;
always@(negedge reset or posedge clk)
begin
if (!reset)
begin
clk_1Hz <= 0;
counter <= 0;
end
else
begin
counter <= counter + 1;
if ( counter == 25_000_000)
begin
counter <= 0;
clk_1Hz <= ~clk_1Hz;
end
end
end
endmodule
沒有留言:
張貼留言