Verilog Code For Fir Filter
Verilog Code For Fir Filter
Verilog Code For Fir Filter
module traffic_lights;
reg clock, red, amber, green;
parameter on = 1, off = 0, red_tics = 30,
amber_tics = 3, green_tics = 20;
initial red = off;
initial amber = off;
// code ends
e=x[i];
end
if(y==32'd2147483648)
z=-z;
end
endmodule
cla4 n5(d1[19:16],b[19:16],clk,c3,sum[19:16],c4);
cla4 n6(d1[23:20],b[23:20],clk,c4,sum[23:20],c5);
//clad 4 module
module cla4(a,b,cin,s,cout);
input[3:0] a,b;
input cin;
output cout;
output[3:0] s;
wire[3:0] g,p;
wire[13:0] z;
xor21 x1 (.a1(a[0]),.a2(b[0]),.z(p[0]));
and21 x2 (.a1(a[0]),.a2(b[0]),.z(g[0]));
xor21 x3 (.a1(a[1]),.a2(b[1]),.z(p[1]));
and21 x4 (.a1(a[1]),.a2(b[1]),.z(g[1]));
xor21 x5 (.a1(a[2]),.a2(b[2]),.z(p[2]));
and21 x6 (.a1(a[2]),.a2(b[2]),.z(g[2]));
xor21 x7 (.a1(a[3]),.a2(b[3]),.z(p[3]));
and21 x8 (.a1(a[3]),.a2(b[3]),.z(g[3]));
xor21 x9 (.a1(cin),.a2(p[0]),.z(s[0]));
and21 x10 (.a1(cin),.a2(p[0]),.z(z[0]));
or21 x11 (.a1(z[0]),.a2(g[0]),.z(z[1]));
xor21 x12 (.a1(z[1]),.a2(p[1]),.z(s[1]));
and31 x13 (.a1(cin),.a2(p[0]),.a3(p[1]),.z(z[2]));
assign z=a1&a2;
endmodule
//or
module or21(a1,a2,z);
input a1,a2; output z;
reg z;
always@(a1,a2)
begin
z<=a1 | a2;
end
endmodule
module or31(a1,a2,a3,z);
input a1,a2,a3; output z;
reg z;
always@(a1,a2,a3)
begin
z<=a1 | a2 | a3;
end
endmodule
module or41(a1,a2,a3,a4,z);
input a1,a2,a3,a4; output z;
reg z;
always@(a1,a2,a3,a4)
begin
z<=a1 | a2 | a3 | a4;
end
endmodule
//code ends here
HALF ADDER
module
halfadder(a,b,sum,carry);
input a,b;
output sum, carry;
wire sum, carry;
assign sum = a^b; // sum bit
assign carry = (a&b) ;
//carry bit
endmodule
FULL ADDER
module fulladder(a,b,c,sum,carry);
input a,b,c;
output sum,carry;
wire sum,carry;
assign sum=a^b^c; // sum bit
assign carry=((a&b) | (b&c) | (a&c)); //carry bit
endmodule
`define TICK #2 //Flip-flop time delay 2 units
module jkflop(j,k,clk,rst,q);
input j,k,clk,rst;
output q;
reg q;
always @(posedge clk)begin
if(j==1 & k==1 & rst==0)begin
q <=`TICK ~q; //Toggles
end
else if(j==1 & k==0 & rst==0)begin
Indians have always proved that it has a great history of great mathematicians . Vedic
maths is one of the discovery of Indians. One i read one such algorithm i decided to
code one such algorithm and check for its speed and compare it with normal
conventional multipliers. Here is a code for a 2 bit vedic multiplier
// code starts here
//////////////////////////////////////////////////////////////////////////////////
// Company:
JK FLIP FLOP
VEDIC MULTIPLIER
// Module Name: vedic_2_x_2
// Target Devices: spartan 6
// Tool versions: Xilinx 13.3
//////////////////////////////////////////////////////////////////////////////////
module vedic_2_x_2(a,b,c );
input [1:0]a;// first input
input [1:0]b;// second input
output [3:0]c;// output
wire [3:0]c;
wire [3:0]temp;
// four multiplication operation of bits according to vedic logic
assign c[0]=a[0]&b[0];
assign temp[0]=a[1]&b[0];
assign temp[1]=a[0]&b[1];
assign temp[2]=a[1]&b[1];
// using two half adders
ha z1(temp[0],temp[1],c[1],temp[3]);
ha z2(temp[2],temp[3],c[2],c[3]);
endmodule
2:4 decoder
// code starts here
module dec2_4 (a,b,en,y0,y1,y2,y3)
input a, b, en;
output y0,y1,y2,y3;
assign y0= (~a) & (~b) & en;
assign y1= (~a) & b & en;
assign y2= a & (~ b) & en;
assign y3= a & b & en;
end module
// code ends
output [2:0]y;
sig y; sig v;
always @ (en, I)
begin
if(en= =0)
8:3 encoder with priority
v=0;
else
v=1;
end
if ( I[7]= =1 & en= =1) y=3b111;
else if ( I[6]==1 & en==1) y=3b110;
else if ( I[5]==1 & en==1) y=3b101;
else if ( I[4]==1 & en==1) y=3b100;
else if ( I[3]==1 & en==1) y=3b011;
else if ( I[2]==1 & en==1) y=3b010;
else if ( I[1]==1 & en==1) y=3b001;
else if ( I[0]==1 & en==1) y=3b000;
else y=3b000;
end
endmodule
// code ends here
//code starts here
module mux8_1
input [7:0]I;
output [2:0]S;
output y;
input en;
reg y;
always @(en,S,I,y);
begin
if (en= =1)
begin
if (s= =000 y=I[0];
else if (s==001) y=I[1];
else if (s==001) y=I[2];
else if (s==001) y=I[3];
else if (s==001) y=I[4];
else if (s==001) y=I[5];
else if (s==001) y=I[6];
else if (s==001) y=I[7];
end
else y=0;
8 TO 1 MULTIPLEXER
end
end
endmodule
//code ends
4-BIT BINARY TO GRAY COUNTER CONVERTER
//code starts here
module b2g(b,g);
input [3:0] b;
output [3:0] g;
xor (g[0],b[0],b[1]),
(g[1],b[1],b[2]),
(g[2],b[2],b[3]);
assign g[3]=b[3];
endmodule
//code ends
DE-MULTIPLEXER ( 1 TO 4)
//code starts here
module demux (s2,s1,I,en,y0,y1,y2,y3)
input s2,s1,I,en;
output y0,y1,y2,y3;
assign y0=(~s2)&(~s1)& I& en;
assign y1=(~s2)& s1& I& en;
assign y2=s2&(~s1)& I & en;
assign y3=s2& s1 & I & en;
//code ends
module srff(s,r,clk,rst, q,qb);
input s,r,clk,rst;
output q,qb;
reg q,qb;
reg [1:0]sr;
always@(posedge clk,posedge rst)
endmodule
SR FLIPFLOP
begin
sr={s,r};
if(rst==0)
begin
case (sr)
2'd1:q=1'b0;
2'd2:q=1'b1;
2'd3:q=1'b1;
UART
module uart ( reset ,txclk , ld_tx_data ,tx_data , tx_enable ,tx_out ,tx_empty ,rxclk
,uld_rx_data ,rx_data ,rx_enable ,rx_in ,rx_empty );
// Port declarations
input reset ;
input txclk ;
input ld_tx_data ;
input [7:0] tx_data ;
input tx_enable ;
output tx_out ;
output tx_empty ;
input rxclk ;
input uld_rx_data ;
output [7:0] rx_data ;
input rx_enable ;
input rx_in ;
output rx_empty ;
// Internal Variables
reg [7:0] tx_reg ;
reg tx_empty ;
reg tx_over_run ;
reg [3:0] tx_cnt ;
reg tx_out ;
reg [7:0] rx_reg ;
reg [7:0] rx_data ;
reg [3:0] rx_sample_cnt ;
reg [3:0] rx_cnt ;
reg rx_frame_err ;
reg rx_over_run ;
reg rx_empty ;
reg rx_d1 ;
reg rx_d2 ;
reg rx_busy ;
// UART RX Logic
always @ (posedge rxclk or posedge reset)
if (reset) begin
rx_reg <= 0;
rx_data <= 0;
rx_sample_cnt <= 0;
rx_cnt <= 0;
rx_frame_err <= 0;
rx_over_run <= 0;
rx_empty <= 1;
rx_d1 <= 1;
rx_d2 <= 1;
rx_busy <= 0;
end else begin
// Synchronize the asynch signal
rx_d1 <= rx_in;
rx_d2 <= rx_d1;
// Uload the rx data
if (uld_rx_data) begin
rx_data <= rx_reg;
rx_empty <= 1;
end
// Receive data only when rx is enabled
if (rx_enable) begin
// Check if just received start of frame
if (!rx_busy && !rx_d2) begin
rx_busy <= 1;
rx_sample_cnt <= 1;
rx_cnt <= 0;
end
// Start of frame detected, Proceed with rest of data
if (rx_busy) begin
rx_sample_cnt <= rx_sample_cnt + 1;
// Logic to sample at middle of data
if (rx_sample_cnt == 7) begin
if ((rx_d2 == 1) && (rx_cnt == 0)) begin
rx_busy <= 0;
end else begin
rx_cnt <= rx_cnt + 1;
// Start storing the rx data
if (rx_cnt > 0 && rx_cnt < 9) begin
rx_reg[rx_cnt - 1] <= rx_d2;
end
if (rx_cnt == 9) begin
rx_busy <= 0;
// Check if End of frame received correctly
if (rx_d2 == 0) begin
rx_frame_err <= 1;
end else begin
rx_empty <= 0;
rx_frame_err <= 0;
// Check if last rx data was not unloaded,
rx_over_run <= (rx_empty) ? 0 : 1;
end
end
end
end
end
end
if (!rx_enable) begin
rx_busy <= 0;
end
end
// UART TX Logic
always @ (posedge txclk or posedge reset)
if (reset) begin
tx_reg <= 0;
tx_empty <= 1;
tx_over_run <= 0;
tx_out <= 1;
tx_cnt <= 0;
end else begin
if (ld_tx_data) begin
if (!tx_empty) begin
tx_over_run <= 0;
end else begin
tx_reg <= tx_data;
tx_empty <= 0;
end
end
if (tx_enable && !tx_empty) begin
tx_cnt <= tx_cnt + 1;
if (tx_cnt == 0) begin
tx_out <= 0;
end
if (tx_cnt > 0 && tx_cnt < 9) begin
tx_out <= tx_reg[tx_cnt -1];
end
if (tx_cnt == 9) begin
tx_out <= 1;
tx_cnt <= 0;
tx_empty <= 1;
end
end
if (!tx_enable) begin
tx_cnt <= 0;
end
end
endmodule
module fsm_full(
Arbiter implemented with 4 requests
clock , // Clock
reset , // Active high reset
req_0 , // Active high request from agent 0
req_1 , // Active high request from agent 1
req_2 , // Active high request from agent 2
req_3 , // Active high request from agent 3
gnt_0 , // Active high grant to agent 0
gnt_1 , // Active high grant to agent 1
gnt_2 , // Active high grant to agent 2
gnt_3 // Active high grant to agent 3
);
// Port declaration here
input clock ; // Clock
input reset ; // Active high reset
input req_0 ; // Active high request from agent 0
input req_1 ; // Active high request from agent 1
input req_2 ; // Active high request from agent 2
input req_3 ; // Active high request from agent 3
output gnt_0 ; // Active high grant to agent 0
output gnt_1 ; // Active high grant to agent 1
next_state = IDLE;
end else begin
next_state = GNT0;
end
GNT1 : if (req_1 == 1'b0) begin
next_state = IDLE;
end else begin
next_state = GNT1;
end
GNT2 : if (req_2 == 1'b0) begin
next_state = IDLE;
end else begin
next_state = GNT2;
end
GNT3 : if (req_3 == 1'b0) begin
next_state = IDLE;
end else begin
next_state = GNT3;
end
default : next_state = IDLE;
endcase
end
always @ (posedge clock)
begin : OUTPUT_LOGIC
if (reset) begin
gnt_0 <= #1 1'b0;
gnt_1 <= #1 1'b0;
gnt_2 <= #1 1'b0;
gnt_3 <= #1 1'b0;
Ram
module ram1(clk,enable,rdwr,address,dtin,dtout);
//parameter N=10;
input clk,enable,rdwr;
input[9:0] address;
input[15:0] dtin;
output [15:0] dtout;
reg[15:0] dtout;
reg[15:0] memory[0:1023];
//reg[15:0] mem;
always@(posedge clk)
begin
if(enable==1'b1)
begin
if(rdwr==1'b0)
begin
memory[address]<=dtin;
end
Memory design -ram/rom
else
dtout<= memory[address];
end
else
dtout<=16'bz;
end
endmodule
Rom
module rom1(clk,romrd,address,dtout);
parameter N=8; input[N-2:0] address;
output tx_transmitter_valid;
reg tx_transmitter;
// Baud generator
wire [BAUD_ACC_WIDTH:0] Baun_Inc;
reg [BAUD_ACC_WIDTH:0] Baud_Acc;
assign Baun_Inc = ((BAUD_RATE << (BAUD_ACC_WIDTH - 4)) + (CLOCK_FREQ
>> 5))
/ (CLOCK_FREQ >> 4);
wire Baud_Pulse = Baud_Acc[BAUD_ACC_WIDTH];
always @ (posedge clock or negedge reset_neg)
begin
if (reset_neg == LOW)
begin
Baud_Acc <= {(BAUD_ACC_WIDTH + 1){LOW}};
end
else if (Present_Processing_Completed == 1'b1) Baud_Acc <=
{(BAUD_ACC_WIDTH + 1){LOW}};
else if (tx_transmitter_valid)
begin
Baud_Acc <= Baud_Acc[BAUD_ACC_WIDTH - 1:0] + Baun_Inc;
end
end
// Transmitter State machine
reg [3:0] State;
wire tx_Xfer_Ready = (State==0);
assign tx_transmitter_valid = ~tx_Xfer_Ready;
reg [7:0] tx_data_reg;
always @ (posedge clock or negedge reset_neg)
begin
if (reset_neg == LOW)
begin
tx_data_reg <= 8'hff;
end
else if (Present_Processing_Completed == 1'b1) tx_data_reg <= 8'hff;
else if (tx_Xfer_Ready & tx_datain_ready)
begin
tx_data_reg <= tx_datain;
end
end
wire [7:0] Tx_Data_Byte;
assign Tx_Data_Byte = REG_INPUT ? tx_data_reg : tx_datain;
always @ (posedge clock or negedge reset_neg)
begin
if (reset_neg == LOW)
begin
State <= 4'b0000;
end
else if (Present_Processing_Completed == 1'b1) State <= 4'b0000;
else
begin
case(State)
4'b0000: if(tx_datain_ready) State <= 4'b0100;
// 4'b0001: if(Baud_Pulse) State <= 4'b0100; // registered input
4'b0100: if(Baud_Pulse) State <= 4'b1000; // start
4'b1000: if(Baud_Pulse) State <= 4'b1001; // bit 0
4'b1001: if(Baud_Pulse) State <= 4'b1010; // bit 1
4'b1010: if(Baud_Pulse) State <= 4'b1011; // bit 2
4'b1011: if(Baud_Pulse) State <= 4'b1100; // bit 3
begin
tx_transmitter <= HIGH;
end
else if (Present_Processing_Completed == 1'b1) tx_transmitter <= HIGH;
else
begin
tx_transmitter <= (State < 4) | (State[3] & MuxBit); // register the
output to make it glitch free
end
end
endmodule
RS232 receiver
module RS232_Receiver (
reset_neg,
clock,
rx_receiver,
rx_dataout_ready,
rx_dataout,
// optional pins for identifying long break in data reception
rx_endofpacket,
rx_Idle,
Exe_LogicImp
);
parameter HIGH = 1'b1;
parameter LOW = 1'b0;
//`ifdef ML401
parameter CLOCK_FREQ = 100000000; // 100MHz
//`else // ifdef ML461
if (reset_neg == LOW)
begin
Rx_Count <= 2'b11;
Rx_Bit <= HIGH;
end
else if (Exe_LogicImp == 1'b1)
begin
Rx_Count <= 2'b11;
Rx_Bit <= HIGH;
end
else if(Baud_Pulse_8x)
begin
if( Rx_Sync[1] && Rx_Count!=2'b11) Rx_Count <= Rx_Count + 2'h1;
else
if(~Rx_Sync[1] && Rx_Count!=2'b00) Rx_Count <= Rx_Count - 2'h1;
if(Rx_Count==2'b00) Rx_Bit <= 1'b0;
else
if(Rx_Count==2'b11) Rx_Bit <= 1'b1;
end
end
reg [3:0] State;
reg [3:0] Bit_Spacing;
// "next_bit" controls when the data sampling occurs
// depending on how noisy the rx_receiver is, different values might work
better
// with a clean connection, values from 8 to 11 work
wire next_bit = (Bit_Spacing==4'd10);
always @(posedge clock or negedge reset_neg)
begin
if (reset_neg == LOW)
begin
Bit_Spacing <= 4'b0000;
end
else if (Exe_LogicImp == 1'b1) Bit_Spacing <= 4'b0000;
else if(State==0)
begin
Bit_Spacing <= 4'b0000;
end
else if(Baud_Pulse_8x)
begin
Bit_Spacing <= {Bit_Spacing[2:0] + 4'b0001} | {Bit_Spacing[3], 3'b000};
end
end
begin
rx_dataout_ready <= (Baud_Pulse_8x && next_bit && State==4'b0001 &&
Rx_Bit); // ready only if the stop bit is received
RxD_data_error <= (Baud_Pulse_8x && next_bit && State==4'b0001 &&
~Rx_Bit); // error if the stop bit is not received
end
end
// Optional functionality for detection of gap if it occurs in the
// received stream of characters.
reg [4:0] gap_count;
always @ (posedge clock or negedge reset_neg)
begin
if (reset_neg == LOW)
begin
gap_count<=5'h00;
end
else if (Exe_LogicImp == 1'b1) gap_count<=5'h00;
else if (State!=0)
begin
gap_count<=5'h00;
end
else if (Baud_Pulse_8x & ~gap_count[4])
begin
gap_count <= gap_count + 5'h01;
end
end
assign rx_Idle = gap_count[4];
reg rx_endofpacket;
always @(posedge clock or negedge reset_neg)
begin
if (reset_neg == LOW)
begin
rx_endofpacket <= LOW;
end
else if (Exe_LogicImp == 1'b1) rx_endofpacket <= LOW;
else
begin
rx_endofpacket <= Baud_Pulse_8x & (gap_count==5'h0F);
end
end
endmodule
module bcd_to_binary (
clk_i,
ce_i,
rst_i,
start_i,
dat_bcd_i,
dat_binary_o,
done_o
);
parameter BCD_DIGITS_IN_PP = 2; // # of digits of BCD input
parameter BITS_OUT_PP = 7; // # of bits of binary output
parameter BIT_COUNT_WIDTH_PP = 3; // Width of bit counter
// I/O declarations
input clk_i; // clock signal
input ce_i; // clock enable input
input rst_i; // synchronous reset
input start_i; // initiates a conversion
input [4*BCD_DIGITS_IN_PP-1:0] dat_bcd_i; // input bus
output [BITS_OUT_PP-1:0] dat_binary_o; // output bus
output done_o; // indicates conversion is done
begin
cin = 1'b0;
for (k=BCD_DIGITS_IN_PP-1; k>=0; k=k-1) // From MS digit to LS digit
begin
digit[3] = 1'b0;
digit[2] = din[4*k+3];
digit[1] = din[4*k+2];
digit[0] = din[4*k+1];
digit_more = digit + 5;
if (cin)
begin
bcd_asr[4*k+3] = digit_more[3];
bcd_asr[4*k+2] = digit_more[2];
bcd_asr[4*k+1] = digit_more[1];
bcd_asr[4*k+0] = digit_more[0];
end
else
begin
bcd_asr[4*k+3] = digit[3];
bcd_asr[4*k+2] = digit[2];
bcd_asr[4*k+1] = digit[1];
bcd_asr[4*k+0] = digit[0];
end
cin = din[4*k+0];
end // end of for loop
end
endfunction
//-------------------------------------------------------------------------// Module code
//-------------------------------------------------------------------------// Perform proper shifting, binary ASL and BCD ASL
assign bin_next = {bcd_reg[0],bin_reg[BITS_OUT_PP-1:1]};
always @(bcd_reg)
begin
bcd_next <= bcd_asr(bcd_reg);
// bcd_next <= bcd_reg >> 1; Just for testing...
end
// Busy bit, input and output registers
always @(posedge clk_i)
begin
if (~rst_i)
begin
busy_bit <= 0; // Synchronous reset
dat_binary_o <= 0;
end
else if (start_i && ~busy_bit)
begin
busy_bit <= 1;
bcd_reg <= dat_bcd_i;
bin_reg <= 0;
end
else if (busy_bit && ce_i && bit_count_done && ~start_i)
begin
busy_bit <= 0;
dat_binary_o <= bin_next;
end
else if (busy_bit && ce_i & ~bit_count_done)
begin
bin_reg <= bin_next;
bcd_reg <= bcd_next;
end
end
assign done_o = ~busy_bit;
// Bit counter
always @(posedge clk_i)
begin
if (~busy_bit) bit_count <= 0;
else if (ce_i && ~bit_count_done) bit_count <= bit_count + 1;
end
assign bit_count_done = (bit_count == (BITS_OUT_PP-1));
endmodule
module crc(clk,din,en,rst,out
);
input clk,din,en,rst;
output reg[4:0]out;
wire c1;
assign c1= din ^out[4];
always @(posedge clk)
begin
if(~rst)
out<=0;
else if(en)
begin
out[4]<=out[3];
out[3]<=out[2];
out[2]<=c1^out[1];
out[1]<=out[0];
out[0]<=c1;
end end
endmodule
Crc parallel :
module crcpar(data,crc_en,rst,clk,crc_out);
input crc_en,rst,clk;
input [7:0]data;
output [4:0]crc_out;
reg [4:0]lfsr_q;
reg [4:0]lfsr_c;
assign crc_out=lfsr_q;
always@(posedge clk)
begin
lfsr_c[0]=lfsr_q[1]^lfsr_q[4]^data[0]^data[3];
lfsr_c[1]=lfsr_q[2]^data[1];
lfsr_c[2]=lfsr_q[1]^lfsr_q[3]^lfsr_q[4]^data[0]^data[2]^data[3];
lfsr_c[3]=lfsr_q[2]^lfsr_q[4]^data[1]^data[3];
lfsr_c[4]=lfsr_q[0]^lfsr_q[3]^data[2];
end
always@(posedge clk)
begin
if(~rst)
begin
lfsr_q<=5'b00000;
end
else if(crc_en)
begin
lfsr_q<=lfsr_c;
//lfsr_q<=crc_en?lfsr_c:lfsr_q;
end
end
endmodule
Binary to Bcd
module bin_bcd(
clk_i,
ce_i,
rst_i,
start_i,
dat_binary_i,
dat_bcd_o,
done_o
);
parameter BITS_IN_PP = 7; // # of bits of binary input
parameter BCD_DIGITS_OUT_PP = 2; // # of digits of BCD output
parameter BIT_COUNT_WIDTH_PP = 3; // Width of bit counter
// I/O declarations
input clk_i; // clock signal
input ce_i; // clock enable input
input rst_i; // synchronous reset
input start_i; // initiates a conversion
input [BITS_IN_PP-1:0] dat_binary_i; // input bus
output [4*BCD_DIGITS_OUT_PP-1:0] dat_bcd_o; // output bus
output done_o; // indicates conversion is done
reg [4*BCD_DIGITS_OUT_PP-1:0] dat_bcd_o;
// Internal signal declarations
reg [BITS_IN_PP-1:0] bin_reg;
reg [4*BCD_DIGITS_OUT_PP-1:0] bcd_reg;
wire [BITS_IN_PP-1:0] bin_next;
reg [4*BCD_DIGITS_OUT_PP-1:0] bcd_next;
reg busy_bit;
reg [BIT_COUNT_WIDTH_PP-1:0] bit_count;
wire bit_count_done;
//-------------------------------------------------------------------------// Functions & Tasks
//-------------------------------------------------------------------------function [4*BCD_DIGITS_OUT_PP-1:0] bcd_asl;
input [4*BCD_DIGITS_OUT_PP-1:0] din;
input newbit;
integer k;
reg cin;
reg [3:0] digit;
reg [3:0] digit_less;
begin
cin = newbit;
for (k=0; k<BCD_DIGITS_OUT_PP; k=k+1)
begin
digit[3] = din[4*k+3];
digit[2] = din[4*k+2];
digit[1] = din[4*k+1];
digit[0] = din[4*k];
digit_less = digit - 5;
if (digit > 4'b0100)
begin
bcd_asl[4*k+3] = digit_less[2];
bcd_asl[4*k+2] = digit_less[1];
bcd_asl[4*k+1] = digit_less[0];
bcd_asl[4*k+0] = cin;
cin = 1'b1;
end
else
begin
bcd_asl[4*k+3] = digit[2];
bcd_asl[4*k+2] = digit[1];
bcd_asl[4*k+1] = digit[0];
bcd_asl[4*k+0] = cin;
cin = 1'b0;
end
end // end of for loop
end
endfunction
begin
busy_bit <= 0;
dat_bcd_o <= bcd_next;
end
else if (busy_bit && ce_i && ~bit_count_done)
begin
bcd_reg <= bcd_next;
bin_reg <= bin_next;
end
end
assign done_o = ~busy_bit;
// Bit counter
always @(posedge clk_i)
begin
if (~busy_bit) bit_count <= 0;
else if (ce_i && ~bit_count_done) bit_count <= bit_count + 1;
end
assign bit_count_done = (bit_count == (BITS_IN_PP-1));
endmodule
Hamming code(h,k) encoder /decoder
Encoder:
module hamen(clk,d,c
);
input clk;
input [3:0] d;
output reg[6:0] c;
always@(posedge clk)
begin
c[6]=d[3];
c[5]=d[2];
c[4]=d[1];
c[3]=d[1]^d[2]^d[3];
c[2]=d[0];
c[1]=d[0]^d[2]^d[3];
c[0]=d[0]^d[1]^d[3];
end
endmodule
Decoder:
module hamd(c,clk,s,c2,d
);
input clk;
input[6:0] c;
output reg[2:0]s;
output reg[6:0] c2;
output reg[3:0]d;
always@(posedge clk)
begin
s[2]=c[0]^c[4]^c[5]^c[6];
s[1]=c[1]^c[2]^c[5]^c[6];
s[0]=c[0]^c[2]^c[4]^c[6];
c2=c;
if(s)
c2[s-1]=~c[s-1];
end
always@(c2)
begin
d[0]=c2[2];
d[1]=c2[4];
d[2]=c2[5];
d[3]=c2[6];
end
endmodule
Sequence detector using FSM flow (with output and RTL)
`tmescale 1ns / 1ps
/////////////////////////////////////////////////////////////////////////////
/////
// Company:
// Engineer:
//
// Create Date: 14:58:32 08/16/2013
// Design Name:
// Module Name: sequence_0111
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
/////////////////////////////////////////////////////////////////////////////
/////
module sequence_0111(clock, reset, in_bit, out_bit
);
input clock, reset, in_bit;
output out_bit;
reg [2:0] state_reg, next_state;
// State declaration
parameter reset_state = 3'b000;
parameter read_zero = 3'b001;
parameter read_0_one = 3'b010;
parameter read_zero_one_one = 3'b011;
parameter read_zero_one_one_one= 3'b100;
// state register
always @ (posedge clock or posedge reset)
if (reset == 1)
state_reg <= reset_state;
else
state_reg <= next_state;
// next-state logic
always @ (state_reg or in_bit)
case (state_reg)
reset_state:
if (in_bit == 0)
next_state = read_zero;
else if (in_bit == 1)
next_state = reset_state;
else next_state = reset_state;
read_zero:
if (in_bit == 0)
next_state = read_zero;
else if (in_bit == 1)
next_state = read_0_one;
else next_state = reset_state;
read_0_one:
if (in_bit == 0)
next_state = read_zero;
else if (in_bit == 1)
next_state = read_zero_one_one;
else next_state = reset_state;
read_zero_one_one:
if (in_bit == 0)
next_state = read_zero;
else if (in_bit == 1)
next_state = read_zero_one_one_one;
else next_state = reset_state;
read_zero_one_one_one:
if (in_bit == 0)
next_state = read_zero;
else if (in_bit == 1)
next_state = reset_state;
else next_state = reset_state;
default: next_state = reset_state;
endcase
assign out_bit = (state_reg == read_zero_one_one_one)? 1 : 0;
endmodule
//test bench
/////////////////////////////////////////////////////////////////////////////
///
// Company:
// Engineer:
//
// Create Date: 15:12:38 08/16/2013
// Design Name: sequence_0111
// Module Name: D:/verilogggggg/sequence_counter_0111/test_ma_sequence.v
// Project Name: sequence_counter_0111
// Target Device:
// Tool versions:
// Description:
//
// Verilog Test Fixture created by ISE for module: sequence_0111
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
/////////////////////////////////////////////////////////////////////////////
///
module test_ma_sequence;
// Inputs
reg clock;
reg reset;
reg in_bit;
// Outputs
wire out_bit;
in_bit = 1;
#10;
reset = 0;
in_bit = 1;
#10;
// Add stimulus here
reset = 1;
in_bit = 1;
#10;
reset = 0;
in_bit = 0;
#10;
reset = 0;
in_bit = 1;
#10;
reset = 0;
in_bit = 1;
#10;
reset = 0;
in_bit = 1;
#10;
end
always begin #5 clock=~clock; end
endmodule
//output waveform