Professional Documents
Culture Documents
ECE 195
At IDLE state, if the input is 56, then it FSM moves to STATE 0. However, if input is something else, it remains at IDLE state. At STATE 0, if the input is 56, it remains there, however, if the input is AB it proceeds to STATE 1, if not among both, the FSM goes back to IDLE state. At STATE 1, if the input is 89, then the FSM moves to STATE 2, else if the input is 56 then it goes back to STATE 0, however if the input is not 89 nor 56 then it goes back to IDLE state. At state 2, if the input is 7F, the FSM moves to state 3, else if the input is 56 the FSM moves to STATE 0, otherwise if neither of both, the it goes back to STATE IDLE. At STATE 3, if the input is 56, then the output is 1, and the FSM moves to STATE 0. However, if the input is something else, then the output is still 1 but the FSM moves to STATE IDLE. This completes the cycle of the FSM Machine. 1 of 11
2 of 11
3 of 11
#2 #2 #2 #2
= = = =
#2 #2 #2 #2
= = = =
#2 #2 #2 #2
= = = =
#2 #2 #2 #2
= = = =
// // // //
1 0 0 1
#2 stream = 8'hAE; #2 stream = 8'hAE; enable = 0; rst = 1; #2 stream = 8'hAE; #2 stream = 8'h11; #2 stream = 8'h22; #2 stream = 8'hD3; // 0 #2 stream = 8'h56; #2 stream = 8'hAB; #2 stream = 8'h89; #2 stream = 8'h7F; // 1 enable = 1; rst = 0; #2 stream = 8'h56; #2 stream = 8'hAB; #2 stream = 8'h89; #2 stream = 8'h7F; // 1 #2 stream = 8'hAE; #2 stream = 8'h11; #2 stream = 8'h22; #2 stream = 8'hD3; // 0 #2 stream = 8'hAA; #2 stream = 8'hBB; #2 stream = 8'hCC; #2 stream = 8'hDD; // 0 $finish; end // this block of code provides clock frequency for the device. always begin #1 clk = !clk; end endmodule
4 of 11
5 of 11
reg[4:0] nxt_state; assign detected = ((state==STATE_3 && nxt_state==SS_IDLE) || (state==STATE_3 && nxt_state==STATE_0)); // the code that follows is heavily based on the finite state machine diagram // in the documentation of this project. please refer to the diagram and its // following discussion on its operation. always@(posedge clk or negedge rst)begin if (rst) begin state <= SS_IDLE; end else begin state <= nxt_state; end end always @(stream) begin if (enable) begin case (state) SS_IDLE: if (stream == PATTERN_0) begin nxt_state = STATE_0; end else begin nxt_state = SS_IDLE; end STATE_0: if (stream == PATTERN_1) begin nxt_state = STATE_1; end else if (stream != PATTERN_0) begin nxt_state = SS_IDLE; end else begin nxt_state = STATE_0; end STATE_1: if (stream == PATTERN_2) begin nxt_state = STATE_2; end else if (stream != PATTERN_0) begin nxt_state = SS_IDLE; end else begin nxt_state = STATE_0; end STATE_2: if (stream == PATTERN_3) begin nxt_state = STATE_3; end else if (stream != PATTERN_0) begin nxt_state = SS_IDLE; end else begin nxt_state = STATE_0; end STATE_3: if (stream == PATTERN_0) begin nxt_state <= STATE_0; end else begin nxt_state <= SS_IDLE; end
6 of 11
default: nxt_state <= SS_IDLE; endcase end else begin state = SS_IDLE; nxt_state <= SS_IDLE; end end endmodule
7 of 11
In this simulation the test bench receives data from an external memory file containing testbench data.
8 of 11
9 of 11
// Initialize number of memory locations to read from memory.mem mem_max = 50; // make sure the initial memory iterator is set to 0. this is the // beginning address. counter = 0; // instantiate the reading of the memory and store data to memory // variable. $readmemh("memory.mem", memory, 0, mem_max); // Initialize Inputs clk = 0; stream = 0; rst = 1; enable = 0; // Wait 3 ns for global rst to finish #3; // turn on the device by setting enable to high and reset to low. enable = 1; rst = 0; // loop at each memory location. then at each address push value to stream at 2 time // units interval. for (counter = 0; counter < mem_max; counter = counter + 1) begin #2 stream = memory[counter]; end $finish; end // block of code provides clock source for the device under test. always begin #1 clk = !clk; end endmodule
10 of 11
11 of 11