林楷宸
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Lab 3 Finite State Machine === > Required time : 12 hour [TOC] --- ## Q1 Sequential Circuit In assignment 4, we have already introduced how DFF worked and how to design a DFF using two latches. Now, let's introduce an alternative way to model DFF. The following code is an example. ```c++ always@(posedge clk) begin if (!rst) begin q <= 1'b0; end else begin q <= d; end end ``` We summarize some points here: - `posedge clk`: only trigger this always block when `clk` is on positvie edges. - `q <= next_q`: <font color=#bf2222>In `DFF` always block, you have to use `<=` instead of `=`.</font> However, you have to use `=` in other always blocks which are not triggered by `clk`. - It is common practice to have no more than **<font color=#bf22222>one</font>** DFF block in a module. - In DFF, `rst` signal is usually used to reset a flip flop. <font color=#bf2222>If we don't mention in this lab, the default reset values in DFF are `0`.</font> - In this lab, reset the DFF, <font color=#bf2222>when `rst = 1'b0`</font>. - In a `DFF` always block, signals will be updated **<font color=#bf2222>simultaneously</font>**. for example: ``` always@(posedge clk) begin if(!rst) begin ... end else begin q1 <= q1 + 1; q2 <= q1; end end ``` Then the signals will be: ``` clk: 1, 2, 3, 4, 5, 6, ----------------------------- q1 : 1, 2, 3, 4, 5, 6 ... q2 : 0, 1, 2, 3, 4, 5 ... (suppose the default value of {q1,q2} are {1,0}) ``` The DFF always block upates `q2` according to previous value of `q1`, not the current `q1`. - Please refer to [FSM](https://hackmd.io/ntyIA5KbSIK3eJTqGSwDYA) for more details. --- In this question, you have to convert the following code by replacing `DFF` module with `DFF` always block. ```c++= module q1_init(clk, rst, in1, in2, out1, out2); input clk, rst, in1, in2; output out1, out2; wire next_out1, next_out2; DFF dff1(.clk(clk), .rst(rst), .d(next_out1), .q(out1)); DFF dff2(.clk(clk), .rst(rst), .d(next_out2), .q(out2)); assign next_out1 = in1 & in2; assign next_out2 = in1 | in2; endmodule module Flip_Flop (clk, rst, d, q); input clk, d; output q; wire n_clk, _q, temp; not no (n_clk, clk); Latch Master ( .clk (n_clk), .d (d), .q (_q) ); Latch Slave ( .clk (clk), .d (_q), .q (temp) ); assign q = (!rst) ? 1'b0 : temp; endmodule module Latch (clk, d, q); input clk; input d; output q; // our design wire not_d, not_q, a1, a2; not n1 (not_d, d); nand na1 (a1, d, clk); nand na2 (a2, not_d, clk); nand na3 (q, a1, not_q); nand na4 (not_q, a2, q); endmodule ``` ```c++= module q1(clk, rst, in1, in2, out1, out2); input clk, rst, in1, in2; output out1, out2; // ... // DFF always@(posedge clk) begin if (!rst) begin out1 <= ...; out2 <= ...; end else begin //...; //...; end end // datapath assign next_out1 = ...; assign next_out2 = ...; endmodule ``` --- ## Q2 Fibonacci Sequence Fibonacci sequence is defined as: $F_{0}=0$ $F_{1}=1$ $F_{n}=F_{n-1}+F_{n-2}$,, $n \geq 2$ Please design a circuit to calculate fibonacci sequence and reset `f` to `32'b1` when `rst = 1'b0`. ```c++= module q2(clk, rst, f); input clk, rst; output [31:0] f; // ... // DFF always@(posedge clk) begin if (!rst) begin f <= 1'b1; fn_1 <= ...; end else begin // hint: definition f <= ...; fn_1 <= ...; end end endmodule ``` ![](https://i.imgur.com/GuyXQee.png) --- ## Q3 Separation of Computation from Sequential DFF Block (<font color=#bf2222>**Important!!!**</font>) Due to the constraints of DFF, we do not suggest to do any computation in a DFF always block. It may easily cause unexpected delay and glitch. As a result, we have to separate the combinational logic (computation) from the sequential DFF block. In this question, you have to divide Q2 into a combinational logic block and a DFF block. ```c++= module q3(clk, rst, f); input clk, rst; output [31:0] f; // ... // DFF always@(posedge clk) begin if (!rst) begin f <= ...; fn_1 <= ...; end else begin f <= next_f; fn_1 <= next_fn_1; end end // datapath assign next_f = ...; assign next_fn_1 = ...; endmodule ``` --- ## Q4 State Transition In sequential circuits, states help us divide a job into small pieces. For example, we can do part A in state 1 and do part B in state 2 ... There are some reasons why we divide a job. One of the reasons is that a job is so complicated that it cannot be finished in a clock cycle, so we devide the job into pieces and do them in multiple clock cycles. Another one is dependency. Sometimes, the signals are not independent, they depends on previous values or other signals( e.g. input signals). As a result, we do certain computation(or action) in the first cycle, and do another one in the second cycle. Condition is also a reason. State transition can decide the next computation (or action), according to the input signals (or previous values). --- ### Part 1 Moore Machine In this question, you are asked to design a circuit that follows the given state transition diagram (It's a moore machine). Reset the state to `S0` when `rst = 1'b0` The output signal `out` is the current state: ``` S0: 2'd0; S1: 2'd1; S2: 2'd2; S3: 2'd3; ``` ![](https://i.imgur.com/9IQjdhN.png) ```c++= `define S0 2'd0 `define S1 2'd1 `define S2 2'd2 `define S3 2'd3 module q4_1(clk, rst, in, out); input clk, rst, in; output [1:0] out; //... // DFF always@(posedge clk) begin if (!rst) begin state <= `S0; end else begin state <= next_state; end end // datapath always@(*) begin case(state) `S0: begin next_state = (in == 1'b1) ? `S0 : `S1; end // ... endcase end // datapath always@(*) begin case(state) `S0: begin out = ...; end // ... endcase end endmodule ``` --- ### Part 2 Mealy Machine Enhance the circuit (`q4_1`) with the following functions. Reset `ans = 32'd0` when `rst = 1'b0`. > The output signal out is the current state. (same as q4_1) ``` {state, in} {S0, 0} : ans = ans + 32'd10; {S0, 1} : ans = ans - 32'd10; {S1, 0} : ans = ans + 32'd87; {S1, 1} : ans = ans + 32'd88; {S2, 0} : ans = 32'd0; {S2, 1} : ans = 32'd5; {S3, 0}: ans = ans * 5; {S3, 1}: ans = ans * 10; ``` ```c++= `define S0 2'd0 `define S1 2'd1 `define S2 2'd2 `define S3 2'd3 module q4_2(clk, rst, in, out, ans); input clk, rst, in; output [1:0] out; output [31:0] ans; //... // DFF always@(posedge clk) begin if (!rst) begin state <= `S0; ans <= 32'd0; end else begin state <= next_state; ans <= next_ans; end end // datapath ... // datapath always@(*) begin case(...) ...: begin next_ans = ...; end ... endcase end endmodule ``` Please answer the following questions in your report. 1. The difference between moore and mealy machine. 2. The reason why part1 is a moore machine. 3. The reason why part2 is a mealy machine. --- ## Q5 Sequence Detector Please design a circuit to detect the sequence `1101`. A sequence detector produces `out = 1` if the consecutive input signals are `1101`; otherwise, `out = 0`.For example, ``` example 1 -----------|.....|-------|.....|------- in : 0,0,0,1,1,0,1,1,0,0,1,1,0,1,0,0... out: 0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0... .................^.............^......... example 2 -----|.....|.....|------------- in : 1,1,0,1,1,0,1,0,0,0,1,0,0... out: 0,0,0,1,0,0,1,0,0,0,0,0,0... ...........^.....^............... ``` ![](https://i.imgur.com/50lV3Zw.png) 1. Draw the state transition diagram on your report. 2. Design the circuit based on the state transition diagram. > Hint: It is a Mealy Machine. ```c++= module q5(clk, rst, in, out); input clk, rst, in; output out; // DFF always@(posedge clk) begin ... // datapath // ... endmodule ``` :::danger 6/1 Hint 2 ![](https://i.imgur.com/Y1HYEo3.png) Note that this state transition diagram is not complete. You have to finish it by yourselves. ::: ## Q6 Sequence Detector 2 A sequential circuit has two inputs, `a` and `b`, and an output `z`. Its function is to compare the input sequences on the two inputs. If `a = b` during any four consecutive clock cycles, the circuit produces `out = 1`; otherwise, `out = 0`. For example, ``` ------|.....|---|.........|----- a : 0,1,1,0,1,1,1,0,0,0,1,1,0 ... b : 1,1,1,0,1,0,1,0,0,0,1,1,1 ... out:0,0,0,0,1,0,0,0,0,1,1,1,0 ... ............^.........^ ^ ^...... ``` ```c++= module q6(clk, rst, a, b, out); input clk, rst, a, b; output out; // DFF always@(posedge clk) begin ... // datapath //... endmodule ``` --- ## Assignment Rule 1. Deadline: <font color=#bf2222>**6/6**</font> 2. Submit your code to ILMS. Please upload the questions separately. <font color=#bf2222>**Do not** zip them</font>. 3. The file content tree should look like: - q1.v - q2.v - q3.v - q4_1.v - q4_2.v - q5.v - q6.v - lab3_StudentID<font color=#bf2222>**.pdf**</font> (ex. `lab1_105066666.pdf`) **<font color=#bf2222>wrong filename = no point.</font>** **<font color=#bf2222>wrong module name = no point.</font>** 4. Answer the question in Q4 and draw the state transition diagramin of Q5 in your report. - Up to 3 pages. 5. We will judge your code by our testbenches (not released testbenches).

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully