吳建興
    • 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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 積體電路設計實驗 Lab02 ```=verilog //rst_n 是什麼? //in_valid ?? //in_p1 ?? //out_valid ?? //circle ?? // value ?? //clk ?? //rst_n ?? //竟然只需要考慮逆時針..... module LOCK( clk, rst_n, //input in_valid, mode, in, in_p1, //output out_valid, circle, value ); //--------------------------------------------------------------------- // PORT DECLARATION //--------------------------------------------------------------------- input clk; input rst_n; input in_valid; input[1:0] mode; input[4:0] in; input[4:0] in_p1; output reg out_valid; output reg[2:0]circle; //output reg[6:0]value; output reg[7:0]value; //--------------------------------------------------------------------- // PARAMETER DECLARATION //--------------------------------------------------------------------- parameter s_waitin = 4'd1; parameter s_shift = 4'd2; parameter s_shift_2 = 4'd3; parameter s_shift_3 = 4'd4; parameter s_sort = 4'd5; parameter s_sum = 4'd6; parameter s_output = 4'd7; //--------------------------------------------------------------------- // LOGIC DECLARATION //--------------------------------------------------------------------- reg [4:0] locks[23:0]; reg [3:0] current_state, next_state; reg [4:0] cnt; reg [1:0] mode_r; reg [4:0] in_p1_r; reg weird; //shift reg [3:0] now_shift_1; reg [3:0] now_shift_2; reg [3:0] now_shift_3; //sort reg sort_idx; reg [7:0] value_array [7:0]; reg [2:0] circle_array[2:0]; // for print array information in local environment integer i, j, k, l; initial begin $dumpfile("LOCK.vcd"); $dumpvars(0, LOCK); for(i = 0; i < 24; i = i + 1) $dumpvars(1, locks[i]); for(i = 0; i < 8; i = i + 1) begin $dumpvars(1, value_array[i]); end for(i = 0;i < 3;i = i + 1) begin $dumpvars(1, circle_array[i]); end end //--------------------------------------------------------------------- // Design //--------------------------------------------------------------------- // FSM // current state always @(posedge clk or negedge rst_n) begin if(!rst_n) current_state <= s_waitin; else current_state <= next_state; end // next state always @(*) begin case(current_state) s_waitin: if (cnt == 5'd24) next_state = s_shift; else next_state = s_waitin; s_shift: if(locks[0] == in_p1_r) begin next_state = s_shift_2; end s_shift_2: if(mode_r == 2'b00 && locks[0] == locks[8]) begin next_state = s_shift_3; end else if(mode_r == 2'b01 && locks[0] == locks[8] && locks[4] == locks[12]) begin next_state = s_shift_3; end else if(mode_r == 2'b10 && locks[0] == locks[8] && locks[4] == locks[12] && locks[2] == locks[10] && locks[6] == locks[14]) begin next_state = s_shift_3; end s_shift_3: if(mode_r == 2'b00 && locks[8] == locks[16]) begin next_state = s_sum; end else if(mode_r == 2'b01 && locks[8] == locks[16] && locks[12] == locks[20]) begin next_state = s_sum; end else if(mode_r == 2'b10 && locks[8] == locks[16] && locks[12] == locks[20] && locks[10] == locks[18] && locks[14] == locks[22]) begin next_state = s_sum; end s_sum: next_state = s_sort; s_sort: if (cnt == 5'd8) next_state = s_output; else next_state = s_sort; s_output: if (cnt == 5'd7) next_state = s_waitin; else next_state = s_output; default: next_state = current_state; endcase end // update counter always @(posedge clk or negedge rst_n) begin if(!rst_n) cnt <= 2'b0; else case(current_state) s_waitin: if (cnt == 5'd24) cnt <= 5'b0; //總共25也是沒問題,可以看看Practice的例子 else if(in_valid) cnt <= cnt + 5'd1; s_sort: if (cnt == 5'd8) cnt <= 5'd0; else cnt <= cnt + 5'd1; s_output: if (cnt == 5'd7) cnt <= 5'd0; else cnt <= cnt + 5'd1; default: cnt <= cnt; endcase end //get mode always @(posedge clk or negedge rst_n) begin if(!rst_n) mode_r <= 2'b0; else case(current_state) s_waitin: if(in_valid && cnt == 0) mode_r <= mode; default: mode_r <= mode_r; endcase end //get in_p1 always @(posedge clk or negedge rst_n) begin if(!rst_n) in_p1_r <= 2'b0; else case(current_state) s_waitin: if(in_valid && cnt == 0) in_p1_r <= in_p1; default: in_p1_r <= in_p1_r; endcase end //initialize always @(posedge clk or negedge rst_n) begin if(!rst_n) for(i = 0;i < 8;i = i+1) value_array[i] <= 8'd0; end //test shift always @(posedge clk or negedge rst_n) begin if(!rst_n) begin now_shift_1 <= 0; now_shift_2 <= 0; now_shift_3 <= 0; end else if (current_state == s_waitin) begin now_shift_1 <= 0; now_shift_2 <= 0; now_shift_3 <= 0; if(in_valid) locks[cnt] <= in; end else begin if(current_state == s_shift) begin if (now_shift_1 == 8) begin now_shift_1 <= 0; end else begin if(!(locks[0] == in_p1_r)) begin for (i = 0;i < 7;i = i + 1) begin locks[i] <= locks[i+1]; end locks[7] <= locks[0]; now_shift_1 <= now_shift_1 + 1; end end end else if(current_state == s_shift_2) begin if(!(mode_r == 2'b00 && locks[0] == locks[8])) begin if(!(mode_r == 2'b01 && locks[0] == locks[8] && locks[4] == locks[12])) begin if(!(mode_r == 2'b10 && locks[0] == locks[8] && locks[4] == locks[12] && locks[2] == locks[10] && locks[6] == locks[14])) begin for (i = 8;i < 15;i = i + 1) begin locks[i] <= locks[i+1]; end locks[15] <= locks[8]; now_shift_2 <= now_shift_2 + 1; end end end end else if(current_state == s_shift_3) begin if(!(mode_r == 2'b00 && locks[8] == locks[16])) begin if(!(mode_r == 2'b01 && locks[8] == locks[16] && locks[12] == locks[20])) begin if(!(mode_r == 2'b10 && locks[8] == locks[16] && locks[12] == locks[20] && locks[10] == locks[18] && locks[14] == locks[22])) begin for (i = 16;i < 23;i = i + 1) begin locks[i] <= locks[i+1]; end locks[23] <= locks[16]; now_shift_3 <= now_shift_3 + 1; end end end end end end //record unlock situation always @(posedge clk or negedge rst_n) begin if(!rst_n) begin for(i = 0;i < 3;i = i+1) circle_array[i] = 7'b111; end else if(current_state == s_waitin) begin for(i = 0;i < 3;i = i+1) circle_array[i] = 7'b111; end else if(current_state == s_sum) begin if(mode_r == 2'b00 && locks[8] == locks[16]) begin if(now_shift_1 <= circle_array[0]) begin circle_array[0] <= now_shift_1; circle_array[1] <= now_shift_2; circle_array[2] <= now_shift_3; value_array[0] <= locks[0] + locks[8] + locks[16]; value_array[1] <= locks[1] + locks[9] + locks[17]; value_array[2] <= locks[2] + locks[10] + locks[18]; value_array[3] <= locks[3] + locks[11] + locks[19]; value_array[4] <= locks[4] + locks[12] + locks[20]; value_array[5] <= locks[5] + locks[13] + locks[21]; value_array[6] <= locks[6] + locks[14] + locks[22]; value_array[7] <= locks[7] + locks[15] + locks[23]; end else if(now_shift_1 == circle_array[0] && now_shift_2 <= circle_array[1]) begin circle_array[0] <= now_shift_1; circle_array[1] <= now_shift_2; circle_array[2] <= now_shift_3; value_array[0] <= locks[0] + locks[8] + locks[16]; value_array[1] <= locks[1] + locks[9] + locks[17]; value_array[2] <= locks[2] + locks[10] + locks[18]; value_array[3] <= locks[3] + locks[11] + locks[19]; value_array[4] <= locks[4] + locks[12] + locks[20]; value_array[5] <= locks[5] + locks[13] + locks[21]; value_array[6] <= locks[6] + locks[14] + locks[22]; value_array[7] <= locks[7] + locks[15] + locks[23]; end else if(now_shift_1 == circle_array[0] && now_shift_2 == circle_array[1] && now_shift_3 <= circle_array[2]) begin circle_array[0] <= now_shift_1; circle_array[1] <= now_shift_2; circle_array[2] <= now_shift_3; value_array[0] <= locks[0] + locks[8] + locks[16]; value_array[1] <= locks[1] + locks[9] + locks[17]; value_array[2] <= locks[2] + locks[10] + locks[18]; value_array[3] <= locks[3] + locks[11] + locks[19]; value_array[4] <= locks[4] + locks[12] + locks[20]; value_array[5] <= locks[5] + locks[13] + locks[21]; value_array[6] <= locks[6] + locks[14] + locks[22]; value_array[7] <= locks[7] + locks[15] + locks[23]; end end else if(mode_r == 2'b01 && locks[8] == locks[16] && locks[12] == locks[20]) begin if(now_shift_1 <= circle_array[0]) begin circle_array[0] <= now_shift_1; circle_array[1] <= now_shift_2; circle_array[2] <= now_shift_3; value_array[0] <= locks[0] + locks[8] + locks[16]; value_array[1] <= locks[1] + locks[9] + locks[17]; value_array[2] <= locks[2] + locks[10] + locks[18]; value_array[3] <= locks[3] + locks[11] + locks[19]; value_array[4] <= locks[4] + locks[12] + locks[20]; value_array[5] <= locks[5] + locks[13] + locks[21]; value_array[6] <= locks[6] + locks[14] + locks[22]; value_array[7] <= locks[7] + locks[15] + locks[23]; end else if(now_shift_1 == circle_array[0] && now_shift_2 <= circle_array[1]) begin circle_array[0] <= now_shift_1; circle_array[1] <= now_shift_2; circle_array[2] <= now_shift_3; value_array[0] <= locks[0] + locks[8] + locks[16]; value_array[1] <= locks[1] + locks[9] + locks[17]; value_array[2] <= locks[2] + locks[10] + locks[18]; value_array[3] <= locks[3] + locks[11] + locks[19]; value_array[4] <= locks[4] + locks[12] + locks[20]; value_array[5] <= locks[5] + locks[13] + locks[21]; value_array[6] <= locks[6] + locks[14] + locks[22]; value_array[7] <= locks[7] + locks[15] + locks[23]; end else if(now_shift_1 == circle_array[0] && now_shift_2 == circle_array[1] && now_shift_3 <= circle_array[2]) begin circle_array[0] <= now_shift_1; circle_array[1] <= now_shift_2; circle_array[2] <= now_shift_3; value_array[0] <= locks[0] + locks[8] + locks[16]; value_array[1] <= locks[1] + locks[9] + locks[17]; value_array[2] <= locks[2] + locks[10] + locks[18]; value_array[3] <= locks[3] + locks[11] + locks[19]; value_array[4] <= locks[4] + locks[12] + locks[20]; value_array[5] <= locks[5] + locks[13] + locks[21]; value_array[6] <= locks[6] + locks[14] + locks[22]; value_array[7] <= locks[7] + locks[15] + locks[23]; end end else if(mode_r == 2'b10 && locks[8] == locks[16] && locks[12] == locks[20] && locks[10] == locks[18] && locks[14] == locks[22]) begin if(now_shift_1 <= circle_array[0]) begin circle_array[0] <= now_shift_1; circle_array[1] <= now_shift_2; circle_array[2] <= now_shift_3; value_array[0] <= locks[0] + locks[8] + locks[16]; value_array[1] <= locks[1] + locks[9] + locks[17]; value_array[2] <= locks[2] + locks[10] + locks[18]; value_array[3] <= locks[3] + locks[11] + locks[19]; value_array[4] <= locks[4] + locks[12] + locks[20]; value_array[5] <= locks[5] + locks[13] + locks[21]; value_array[6] <= locks[6] + locks[14] + locks[22]; value_array[7] <= locks[7] + locks[15] + locks[23]; end else if(now_shift_1 == circle_array[0] && now_shift_2 <= circle_array[1]) begin circle_array[0] <= now_shift_1; circle_array[1] <= now_shift_2; circle_array[2] <= now_shift_3; value_array[0] <= locks[0] + locks[8] + locks[16]; value_array[1] <= locks[1] + locks[9] + locks[17]; value_array[2] <= locks[2] + locks[10] + locks[18]; value_array[3] <= locks[3] + locks[11] + locks[19]; value_array[4] <= locks[4] + locks[12] + locks[20]; value_array[5] <= locks[5] + locks[13] + locks[21]; value_array[6] <= locks[6] + locks[14] + locks[22]; value_array[7] <= locks[7] + locks[15] + locks[23]; end else if(now_shift_1 == circle_array[0] && now_shift_2 == circle_array[1] && now_shift_3 <= circle_array[2]) begin circle_array[0] <= now_shift_1; circle_array[1] <= now_shift_2; circle_array[2] <= now_shift_3; value_array[0] <= locks[0] + locks[8] + locks[16]; value_array[1] <= locks[1] + locks[9] + locks[17]; value_array[2] <= locks[2] + locks[10] + locks[18]; value_array[3] <= locks[3] + locks[11] + locks[19]; value_array[4] <= locks[4] + locks[12] + locks[20]; value_array[5] <= locks[5] + locks[13] + locks[21]; value_array[6] <= locks[6] + locks[14] + locks[22]; value_array[7] <= locks[7] + locks[15] + locks[23]; end end end else if (current_state == s_sort) begin if(~sort_idx) begin for(i = 0;i < 8;i = i + 2) begin value_array[i] <= (value_array[i] < value_array[i+1]) ? value_array[i+1] : value_array[i]; value_array[i+1] <= (value_array[i] < value_array[i+1]) ? value_array[i]:value_array[i+1]; end end else begin for(i = 1;i < 7;i = i + 2) begin value_array[i] <= (value_array[i] < value_array[i+1]) ? value_array[i+1] : value_array[i]; value_array[i+1] <= (value_array[i] < value_array[i+1]) ? value_array[i]:value_array[i+1]; end end end end // sort always @(posedge clk or negedge rst_n) begin if(!rst_n) sort_idx <= 1'b0; else if(current_state == s_sort) sort_idx <= ~sort_idx; end //output always @(posedge clk or negedge rst_n) begin if(!rst_n) out_valid <= 1'b0; else case(current_state) s_output: out_valid <= 1'b1; default: out_valid <= 1'b0; endcase end always @(posedge clk or negedge rst_n) begin if(!rst_n) circle <= 3'b0; else case(current_state) s_output: if (cnt < 5'd4) circle <= circle_array[cnt]; else circle <= 3'b0; default: circle <= 3'b0; endcase end always @(posedge clk or negedge rst_n) begin if(!rst_n) value <= 8'b0; else case(current_state) s_output: value <= value_array[cnt]; //s_output: value <= 8'b0; default: value <= 8'b0; endcase end endmodule ``` ## 重點整理 1. 一個 always block 可以對多個 reg 賦值,但是一個 reg 只能在一個 always block 被賦值

    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