2014年6月1日 星期日

Verilog (http://www.see.ed.ac.uk/)

http://www.see.ed.ac.uk/~gerard/Teach/Verilog/manual/index.html

Contents



  1. Introduction
    1. Verilog
    2. The Manual
    3. Gate Types
  2. Lexicography
    1. White Space and Comments
    2. Operators
    3. Numbers
    4. Strings
  3. DataTypes
    1. Nets
    2. Registers
    3. Vectors
    4. Numbers
    5. Arrays
    6. Tri-state
  4. Operators
    1. Arithmetic
    2. Logical
    3. Relational
    4. Equality
    5. Bitwise
    6. Reduction
    7. Shift
    8. Concatenation and Replication
  5. System Tasks
    1. Output
    2. Monitoring a Stimulation
    3. Stopping a Simulation
  6. Large Worked Example: Multiplexor
    1. Introduction and Logic diargram
    2. Breakdown of Gate Level Description
    3. Breakdown of Logic Level Description
    4. Breakdown of Case Description
    5. Conditional Operator Implementation
    6. Stimulus for Multiplexor
  7. Modules
    1. Modules
    2. Stimulus
  8. Ports
    1. Port Lists
    2. Port Connections
  9. Basic Blocks
    1. Introduction to Procedural Contructs
    2. The initial Block
    3. The always Block
  10. Large Worked Example : Binary Counter
    1. Introduction and Logical Diagram
    2. Gate Level Description
    3. Behavioral Description
    4. The John Cooley Challenge
  11. Timing Control
    1. Delay Based
    2. Event Based
    3. Sensitivity (Trigger) List
    4. Gates : Information Propagation Delays
  12. Branches
    1. If-else
    2. Case Statement
    3. The Conditional Operator
  13. Loops
    1. Introduction to Looping Constructs
    2. While Loop
    3. For Loop
    4. Repeat Loop
    5. Forever Loop
  14. Extras
    1. Opening Files
    2. Writing to a File
    3. Closing a File
    4. Manipulating Memories Files
  15. Appendices
    1. Operator Precedance
    2. Keywords
    3. System Tasks and Functions
    4. Nets Types
    5. Creating Input Vectors

Verilog DataTypes資料型態

  1. DataTypes  源自於 http://www.see.ed.ac.uk/~gerard/Teach/Verilog/manual/
    1. Nets
    2. Registers
    3. Vectors
    4. Numbers
    5. Arrays
    6. Tri-state

Nets

Keywords: wire, supply0, supply1
default value: z
default size: 1 bit
Nets represent the continuous updating of outputs with respect to their changing inputs. For example in the figure below, c is connected to a by a not gate. if c is declared and initialised as shown, it will continuously be driven by the changing value of a, its new value will not have to be explicitly assigned to it.

If the drivers of a wire have the same value, the wire assumes this value. If the drivers have different values it chooses the strongest, if the strengths are the same the wire assumes the value of unknown, x.
The most frequently used net is the wire, two others which may be useful are supply0, and supply1, these model power supplies in a circuit.



Registers

Keywords: reg
default value: x
default size: 1 bit
The fundamental difference between nets and registers is that registers have to be assigned values explicitly. That value is held until a new assignment is made. This property can, for example, be used to model a E-type flip flop as shown in figure below, with corresponding Verilog code given below.



        module E_ff(q, data, enable, reset, clock);
            output q;
            input data, enable, reset, clock;
            reg q;

            always @(posedge clock)  // whenever the clock makes a transition to 1
                if (reset == 0)
                 q = 1'b0;
                else if (enable==1)
                 q = data;
                // implicitly : else q = q:

        endmodule

Register q holds the same value until it us changed by an explicit assignment.

As a contrast, if we go into a higher level module, for example the stimulus shown below, the output from the E_ff would have to be assigned as a net so that the E_ff module can drive its value. So now q is a wire.
        module stimulus;
          reg data, enable, clock, reset;
          wire q;

        initial begin
          clock = 1'b0;
          forever #5 clock = ~clock;
        end

        E_ff eff0(q, data, enable, reset, clock);
        // as with 'c' in the previous section, the wire 'q' will now have its
        // value driven into it by the E_ff module.

        initial begin
          reset = 1'b0;
          #10 reset = 1'b1;
              data = 1'b1;
          #20 enable = 1;
          #10 data = 1'b0;
          #10 data = 1'b1;
          #10 enable = 0;
          #10 data = 1'b0;
          #10 data = 1'b1;
          #10 enable = 1;
          #10 reset = 1'b0;
          #30 $finish;
        end

        initial
          $monitor($time, " q = %d", q);

        endmodule

EXERCISE
Consider the stimulus above and predict the output for q. Then stimulus and check your answer.



Vectors

Both the register and net data types can be any number of bits wide if declared as vectors. Vectors can be accessed either in whole or in part, the left hand number is always the most significant number in the vector. See below for examples of vector declarations.
        reg [3:0] output; // output is a 4-bit register
        wire [31:0] data; // data is a 32-bit wire
        reg [7:0] a;

        data[3:0] = output; // partial assignment
        output = 4'b0101;   // assignment to the whole register


It is important to be consistant in the ordering of the vector width declaration. Normally the most significant figure is written first.
        reg [3:0] a;  // it is important to adopt one convention for
        reg [0:3] b;  // the declaration of vector width.

EXERCISE
a) Declare an 8 bit wire with variable name address.
b) Assign 4'b1010 to its 4 most signficant bits.



Numbers

Integers

Keywords: integer
default value: x
default size: dependant on the host machine, but at least 32 bits
Integers are similar to registers but can store signed (i.e. negative as well as positive numbers) whereas registers can only store positive numbers.

Real numbers

Keywords: real
default value: x
default size: again host machine dependant, but at least 64 bits
Real numbers can be in decimal or scientific format as shown in the example below. When written with a decimal point, there must be at least one number on either side of the point. A real number is converted to an integer by rounding to the nearest integer.
        // 1.3    a real number in decimal format
        // 1.3e27 a real number in scientific format

        real pedantic_pi;
        integer relaxed_pi;

        initial begin
           pedantic_pi = 3.141596259;
           relaxed_pi = pedantic_pi; // relaxed_pi is set to 3
        end

A warning about using registers vs. integers for signed values
An arithmetic operation is treated differently depending on the data type of the operand. A register operand is treated as an unsigned value and an integer value is treated as a signed value. Therefore if a negative value, such as -4'd12, is assigned to a register, it will stored as a positive integer which is its 2's complement value. So when used as an operand the 2's complement value will be used causing unintentional behaviour. If stored in an integer, the behaviour would be as expected, using signed arithmetic.



Arrays

Registers, integers and time data types can be declared as arrays, as shown in the example below. Note the size of the array comes after the variable name in the declaration and after the variable name but before the bit reference in an assignment. So :-
declaration: <data_type_spec> {size} <variable_name> {array_size}
reference: <variable_name> {array_reference} {bit_reference}
        reg data [7:0]; // 8 1-bit data elements
        integer [3:0] out [31:0]; // 32 4-bit output elements

        data[5]; // referencing the 5th data element



Memories

Memories are simply an array of registers. The syntax is the same as above, we will discuss modelling RAM and ROM using memories in a later section.
        reg [15:0] mem16_1024 [1023:0]; // memory mem16_1024 is 1K of 16 bit elements
        mem16_1024[489]; // referencing element 489 of mem16_1024

It is always good practice to use informative names like mem16_1024 to help keep track of memories.
EXERCISE
Instantiated a 2k memory of 8 bit elements.
Answers




Tri-state

A tri-state driver is one which will output either HIGH, LOW or "nothing".
In some architectures, many different modules need to be able to put data onto (to drive) the same bus, at different times. Thus they all connect to the one common bus - but a set of control signals seek to ensure that only one of them is driving a signal at any one time.
In Verilog, this is modelled using different signal "strengths". There is a signal value: z, which is called "high-impedance". This basically means that a node is isolated, that is not driven. It is possible to assign this value to a net.
Normally if two values are simultaneously written to a net, the result is unknown: x; however, if a driven value is also assigned to the same net as a high-impedance value, the driven value will over-ride the z. This is the basis for the following tri-state driver:
        module triDriver(bus, drive, value);
           inout [3:0] bus;
           input       drive;
           input [3:0] value;

           assign #2 bus = (drive == 1) ? value : 4'bz;

        endmodule // triDriver

When the drive signal is high, the bus is driven to the data value, otherwise, this driver outputs only a high-impedance and hence can be over-ridden by any other driven value.

NOTE: the bus is a wire and is designated as an inout variable on the port declarations.
The following example shows the effect of several control combinations on three tri-state buffers:
        module myTest;
           wire [3:0] bus;
           reg          drive0, drive1, drive2;

           integer    i;

           triDriver mod1 (bus, drive0, i[3:0]);
           triDriver mod2 (bus, drive1, 4'hf);
           triDriver mod3 (bus, drive2, 4'h0);

           initial begin
              for (i = 0; i < 12; i = i + 1) begin
                 #5 {drive2, drive1, drive0} = i;
                 #5 $display ($time,"  %b %b %d", i[2:0], bus, bus);
              end
              $finish;
           end // initial begin

        endmodule // myTest

giving output:
                   10  000 zzzz  z
                   20  001 0001  1
                   30  010 1111 15
                   40  011 xx11  X
                   50  100 0000  0
                   60  101 0x0x  X
                   70  110 xxxx  x
                   80  111 xxxx  x
                   90  000 zzzz  z
                  100  001 1001  9
                  110  010 1111 15
                  120  011 1x11  X

Verilog Operands (運算元)

An expression comprises of operators and operands. Data objects form the operands of an expression and it is their value that is used by operators in an expression.
literalsstring (bit & character)
numeric
real
identifiers

module
parameter
wire
register
macros (text substitutions)
index & slice names
function calls



12'b0011_0101_1100-12-bit sized binary constant number
2'O57-2 digit octal number
3_14159-default decimal number
4'h9FDE-4 digit hexadecimal number

module Literals (A1, A2, B1, B2, Y1, Y2);
      input A1, A2, B1, B2;
      output [7:0] Y1;
      output [5:0] Y2;

      parameter CST=4'b1010;
      parameter twentyfive=25; //numeric literal
      reg [7:0] Y1;
      reg [5:0] Y2;
    always @(A or A2 or B1 or B2 or Y1 or Y2)
    begin
      if (A1==1)
      //parenthesis are needed to be syntatically correct
        Y1={CST,4'b0101}; //bit string literal
      else if (A2==1)
        Y1={CST,4'b0111}; //bit string literal
      else
        Y1={CST,4'b1111}; //bit string literal

      if (B==0)
        Y2=10; //integer literal
      else if (B2==1)
        Y2=15; //integer literal
      else
        Y2=twentyfive+10+15; //integer literal
    end
endmodule



module Identifier (A, B, C, D, E, Y1, Y2);

      input A, B, C, D; //identifiers
      input [7:0] E;
      output Y1, Y2;
      reg F, Y1, Y2; //identifiers

    function AND_OR_Bits;
      input [7:0] A;
      begin
        AND_OR_Bits=(A[7] & A[6] & A[5] & A[4]) & (A[3] | A[2] | A[1] | A[0]);
      end
    endfunction

      always @(A or B or C or D or E)
      begin
        F=A&B&AND_OR_Bits(E);
        Y1=C&F;
        Y2=D|F; 
      end
endmodule



module Functional_Calls (A1, A2, A3, A4, B1, B2, Y1, Y2);

      input A1, A2, A3, A4, B1, B2;
      output Y1, Y2;
      reg Y1, Y2; 

      always @(A1 or A2 or A3 or A4 or B1 or B2)
      begin
        Y1=function1(A1,A2,A3,A4)|B1|B2;
        Y2=B1||B2||function1(A1,A2,A3,A4); 
      end
       function function1;
      input F1, F2, F3, F4;
      begin
        function1=(F1 & F2) | (F3 & F4);
      end
    endfunction

endmodule


module Index_Slice_Name (A, B, Y);

      input [5:0] A, B;
      output [11:0] Y;
      parameter C=3'b100; reg [11:0] Y; 

      always @(A or B)
      begin
        Y[2:0]=A[0:2]; //swap bits
        Y[3]=A[3]&B[3]; //single index
        Y[5:4]={A[5]&B[5],A[4]|B[4]};
        Y[8:6]=B[2:0]; //3-bit slice
        Y[11:9]=C;
      end
endmodule

Verilog Operators 運算子(運算式)

Operators   (https://www.utdallas.edu/~kad056000/index_files/verilog/verilogoperator.html)


Operators perform an opeation on one or more operands within an expression. An expression combines operands with appropriate operators to produce the desired functional expression.

Groups of Verilog operators are shown on the left. The table shows the operators in descending order of precedence. Operators with equal precedence are shown grouped.


Verilog OperatorNameFunctional Group
[ ]bit-select or part-select
( )parenthesis
!
~
&
|
~&
~|
^
~^ or ^~
logical negation
negation
reduction AND
reduction OR
reduction NAND
reduction NOR
reduction XOR
reduction XNOR
logical
bit-wise
reduction
reduction
reduction
reduction
reduction
reduction
+
-
unary (sign) plus
unary (sign) minus
arithmetic
arithmetic
{ }concatenationconcatenation
{{ }}replicationreplication
*
/
%
multiply
divide
modulus
arithmetic
arithmetic
arithmetic
+
-
binary plus
binary minus
arithmetic
arithmetic
<<
>>
shift left
shift right
shift
shift
>
>=
<
<=
greater than
greater than or equal to
less than
less than or equal to
relational
relational
relational
relational
==
!=
logical equality
logical inequality
equality
equality
===
!==
case equality
case inequality
equality
equality
&bit-wise ANDbit-wise
^
^~ or ~^
bit-wise XOR
bit-wise XNOR
bit-wise
bit-wise
|bit-wise ORbit-wise
&&logical ANDlogical
||logical ORlogical
?:conditionalconditional



module Arithmatic (A, B, Y1, Y2, Y3, Y4, Y5);
      input [2:0] A, B;
      output [3:0] Y1;
      output [4:0] Y3;
      output [2:0] Y2, Y4, Y5;
      reg [3:0] Y1;
      reg [4:0] Y3;
      reg [2:0] Y2, Y4, Y5; 
    always @(A or B)
    begin
      Y1=A+B;//addition
      Y2=A-B;//subtraction
      Y3=A*B;//multiplication
      Y4=A/B;//division
      Y5=A%B;//modulus of A divided by B 
    end
endmodule

module Sign (A, B, Y1, Y2, Y3);

      input [2:0] A, B;
      output [3:0] Y1, Y2, Y3;
      reg [3:0] Y1, Y2, Y3; 
    always @(A or B)
    begin
      Y1=+A/-B;
      Y2=-A+-B;
      Y3=A*-B; 
    end
endmodule


module Relational (A, B, Y1, Y2, Y3, Y4);

      input [2:0] A, B;
      output Y1, Y2, Y3, Y4;
      reg Y1, Y2, Y3, Y4; 
    always @(A or B)
    begin
      Y1=A<B;//less than
      Y2=A<=B;//less than or equal to
      Y3=A>B;//greater than
      if (A>B)
        Y4=1;
      else
        Y4=0;
    end
endmodule

module Equality (A, B, Y1, Y2, Y3);

      input [2:0] A, B;
      output Y1, Y2;
      output [2:0] Y3;
      reg Y1, Y2;
      reg [2:0] Y3; 
    always @(A or B)
    begin
      Y1=A==B;//Y1=1 if A equivalent to B
      Y2=A!=B;//Y2=1 if A not equivalent to B
      if (A==B)//parenthesis needed
        Y3=A;
      else
        Y3=B;
    end
endmodule

module Logical (A, B, C, D, E, F, Y);

      input [2:0] A, B, C, D, E, F;
      output Y;
      reg Y; 
    always @(A or B or C or D or E or F)
    begin
      if ((A==B) && ((C>D) || !(E<F)))
        Y=1;
      else
        Y=0;
    end
endmodule


module Bitwise (A, B, Y);

      input [6:0] A;
      input [5:0] B;
      output [6:0] Y;
      reg [6:0] Y; 
    always @(A or B)
    begin
      Y(0)=A(0)&B(0); //binary AND
      Y(1)=A(1)|B(1); //binary OR
      Y(2)=!(A(2)&B(2)); //negated AND
      Y(3)=!(A(3)|B(3)); //negated OR
      Y(4)=A(4)^B(4); //binary XOR
      Y(5)=A(5)~^B(5); //binary XNOR
      Y(6)=!A(6); //unary negation 
    end
endmodule


module Shift (A, Y1, Y2);

      input [7:0] A;
      output [7:0] Y1, Y2;
      parameter B=3; reg [7:0] Y1, Y2; 
    always @(A)
    begin
      Y1=A<<B; //logical shift left
      Y2=A>>B; //logical shift right 
    end
endmodule


module Concatenation (A, B, Y);

      input [2:0] A, B;
      output [14:0] Y;
      parameter C=3'b011;
      reg [14:0] Y; 
    always @(A or B)
      Y={A, B, (2{C}}, 3'b110};
    end
endmodule

module Reduction (A, Y1, Y2, Y3, Y4, Y5, Y6);

      input [3:0] A;
      output Y1, Y2, Y3, Y4, Y5, Y6;
      reg Y1, Y2, Y3, Y4, Y5, Y6; 
    always @(A)
    begin
      Y1=&A; //reduction AND
      Y2=|A; //reduction OR
      Y3=~&A; //reduction NAND
      Y4=~|A; //reduction NOR
      Y5=^A; //reduction XOR
      Y6=~^A; //reduction XNOR 
    end
endmodule

module Conditional (Time, Y);

      input [2:0] Time;
      output [2:0] Y;
      reg [2:0] Y;
      parameter Zero =3b'000;
      parameter TimeOut = 3b'110; 
    always @(Time)
    begin
      Y=(Time!=TimeOut) ? Time +1 : Zero;
    end
endmodule




FSM 顯示HELLO ----適用於DE2-70

設計一個迴圈顯示HELLO的電路。字元從右向左移動。
用8個7-bit的寄存器組成pipeline。每個寄存器的輸出直接驅動7-segment。設計一個FSM控制pipeline:
1) 系統重定後的前8個時鐘,插入正確的字元(HELLO)。
2) 完成1)後,將pipeline的最後一個寄存器的輸出回饋到第一個寄存器的輸入,建立迴圈。

3) 間隔約1s迴圈顯示HELLO


//part6 間隔約1s迴圈顯示HELLO

module FSM_part6(
                input [0:0] KEY,       //rst_n
                input CLOCK_50,        //50 MHz
                output [0:6] HEX7,HEX6,HEX5,HEX4,HEX3,HEX2,HEX1,HEX0,
                output [8:0]LEDG
                );
                
wire clk,rst_n,clk_1;
reg [3:0] q,d;      //FSM的現態和次態
reg [0:6] char;     //重定後前8個時鐘pipeline寄存器的輸入,也是迴圈顯示的內容

reg pipe_s;    //迴圈啟動
wire [0:6] pipe_in,pipe_o0,pipe_o1,pipe_o2,pipe_o3,pipe_o4,pipe_o5,pipe_o6,pipe_o7;
                 //pipeline寄存器的輸入和輸出
            
reg [25:0] cnt;    //用於分頻的計數器            
reg LED;

parameter S0=4'd0,S1=4'd1,S2=4'd2,S3=4'd3,S4=4'd4,S5=4'd5,S6=4'd6,S7=4'd7,S8=4'd8;   
                   //狀態:初始8個,啟動pipeline寄存器後1個
parameter H=7'b1001000,
          E = 7'b0110000, 
          L = 7'b1110001, 
          O = 7'b0000001, 
          Blank = 7'b1111111;

assign rst_n=KEY[0];
assign clk=CLOCK_50;

//分頻,產生約1s的時鐘clk_1
always @(posedge clk)
begin
    if(!rst_n)
        cnt<=1'b0;
    else    
        cnt<=cnt+1;
        
    if (cnt<=26'd25_000_000)
        LED<=1'b0;
    else
        LED<=1'b1;       
       
        
end        
assign clk_1=~|cnt;        //產生約1s的時鐘    

assign LEDG[8]=LED;


//狀態轉換
always @(posedge clk)
begin
    if(!rst_n)
        q<=S0;
    else
        q<=d;
end

//狀態表
always @(q,clk_1)
begin
    case(q)
    S0:
        if(clk_1)
            d<=S1;
        else
            d<=S0;
    S1:
        if(clk_1)
            d<=S2;
        else
            d<=S1;
    S2:
        if(clk_1)
            d<=S3;
        else
            d<=S2;
    S3:
        if(clk_1)
            d<=S4;
        else
            d<=S3;
    S4:
        if(clk_1)
            d<=S5;
        else
            d<=S4;
    S5:
        if(clk_1)
            d<=S6;
        else
            d<=S5;
    S6:
        if(clk_1)
            d<=S7;
        else
            d<=S6;
    S7:
        if(clk_1)
            d<=S8;
        else
            d<=S7;
    S8:
        d<=S8;
    default:
        d<=4'bxxxx;
    endcase
end

//每種狀態的輸出
always @(q)
begin
    pipe_s=1'b0;
    char=7'bxxx_xxxx;
    case(q)
    S0:
        char=H;
    S1:
        char=E;
    S2:
        char=L;
    S3:
        char=L;
    S4:
        char=O;
    S5:
    char=Blank;
    S6:
    char=Blank;
    S7:
        char=Blank;
    S8:
        pipe_s=1'b1;    //啟動迴圈顯示
    default:
        d=4'bxxxx;
    endcase
end

assign pipe_in=(pipe_s==1'b1)?pipe_o7:char;

//pipeline寄存器
reg_p r0(pipe_in,clk,rst_n,clk_1,pipe_o0);
reg_p r1(pipe_o0,clk,rst_n,clk_1,pipe_o1);
reg_p r2(pipe_o1,clk,rst_n,clk_1,pipe_o2);
reg_p r3(pipe_o2,clk,rst_n,clk_1,pipe_o3);
reg_p r4(pipe_o3,clk,rst_n,clk_1,pipe_o4);
reg_p r5(pipe_o4,clk,rst_n,clk_1,pipe_o5);
reg_p r6(pipe_o5,clk,rst_n,clk_1,pipe_o6);
reg_p r7(pipe_o6,clk,rst_n,clk_1,pipe_o7);

assign HEX0=pipe_o0,
       HEX1=pipe_o1,
       HEX2=pipe_o2,
       HEX3=pipe_o3,
       HEX4=pipe_o4,
       HEX5=pipe_o5,
       HEX6=pipe_o6,
       HEX7=pipe_o7;

endmodule

module reg_p(
                input [0:6] r,
                input clk,rst_n,e,
                output reg [0:6] q
                );
                
always @(posedge clk)
begin
    if(!rst_n)
        q<=7'b111_1111;
    else if(e)
        q<=r;
end

endmodule

FSM 紅綠燈--適用於DE2-70 ( EP2C35F672C6)

利用FSM 狀態機啟動紅綠燈 當SET=SW[0]=1時綠燈亮停1秒鐘,再轉為黃燈亮停2秒鐘,再轉為cj紅燈亮停5秒鐘,再轉為綠燈(初始狀態)




//FSM 紅綠燈--適用於DE2-70 ( EP2C35F672C6)

module FSM2(CLOCK_50, KEY[1:0],SW[1:0],LEDR[2:0]); //  LEDR[2:0]= green, yellow, red);

// module FSM2(clock, rst_n, set, green, yellow, red);
input CLOCK_50;     //clock;
input [1:0]KEY; //rst_n;
input [1:0]SW;   //set;
output [2:0] LEDR; //LEDR[2] green;  LEDR[1]=yellow; LEDR[0]=red;


reg [1:0] state, next_state;
reg green, yellow, red;

reg [4:0] count;
reg reset_count;

assign LEDR[0]=green;
assign LEDR[1]=yellow;
assign LEDR[2]=red;

wire rst_n ,clock_4Hz ,set ;
assign rst_n=KEY[0];
assign set=SW[0];

clock_div  u0(.CLOCK_50(CLOCK_50),.clock_4Hz(clock_4Hz) );
//控制器的狀態機(分開寫)

always@(posedge clock_4Hz, negedge rst_n)
begin
if(!rst_n) state <= 2'd0;
else state <= next_state;
 end

////////////////////////////////////
always@(state or set or count)
begin
case(state)
 2'd0:
if(set == 1'b1)
next_state=2'd1;
else
next_state=2'd0;
 2'd1:
if( count==5'd4 )
next_state=2'd2;
else
next_state=2'd1;
2'd2:
if( count==5'd8 )
next_state=2'd0;
else
next_state=2'd2;
  default:
next_state = 2'd0;
endcase
 end


//在每個狀態下,輸出的號誌燈訊號
always@(state)
begin
        case(state)
        2'd0: begin
                   green =1'b1;
                   yellow =1'b0;
                   red =1'b0;
                  end

        2'd1: begin
                   green =1'b0;
                   yellow =1'b1;
                   red =1'b0;
                  end

        2'd2: begin
                   green =1'b0;
                   yellow =1'b0;
                   red =1'b1;
                  end

    default:begin
                   green =1'b0;
                   yellow =1'b0;
                   red =1'b0;
                  end
        endcase
end
//重新啟動計數器的裝置
always@(posedge clock_4Hz)
  begin
    if(state!=next_state)
      reset_count <= 1'b1;
    else
      reset_count <= 1'b0;
  end

//計數器
always@(posedge clock_4Hz or posedge reset_count)

  if(reset_count == 1'b1)
    count <= 5'd0;
  else
    count <= count + 5'd1;

endmodule








module clock_div( CLOCK_50, clock_4Hz );
input CLOCK_50;
output clock_4Hz;
reg [25:0] count_4Hz;
reg clock_4Hz;
//====================================
// 50MHz => 4 Hz
//====================================
always@(posedge CLOCK_50)
begin
if( count_4Hz < 26'd12_500_000 )
  count_4Hz <= count_4Hz + 26'd1;
else
  count_4Hz <= 26'd00_000_001;
 end
always@(posedge CLOCK_50)
begin
  if( count_4Hz <= 26'd6_250_000 )
    clock_4Hz <= 1'd1;
else
    clock_4Hz <= 1'd0;
end
endmodule

Quartus II 9.1和DE2基本使用方法----入門(3) 數位比較器的設計與模擬

入門(3) 數位比較器的設計與模擬

1). 適用於DE2-70 的8bit 比較器的設計(可以下載到DE2-70)

module comp_8bit(

input [17:0]SW,
input [3:0] KEY,
output [17:0] LEDR,
output [7:0] LEDG,
output [6:0] HEX0,
output [6:0] HEX1,
output [6:0] HEX2,
output [6:0] HEX3,
output [6:0] HEX4,
output [6:0] HEX5,
output [6:0] HEX6,
output [6:0] HEX7
);
assign HEX0=7'b111_1111; //off 7-segment Display
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 [7:0]A,B;
reg [2:0]Y ;

assign A=SW[7:0];
assign B=SW[15:8];
assign LEDR[2:0]=Y;

always@(A,B)
begin
  if (A==B)
     begin
      Y[0]=0;
      Y[1]=1;
      Y[2]=0;  //{0,1,0};
     end      
  else if (A>B)
     begin
      Y[1]=0;
      Y[0]=1;
      Y[2]=0;  //{1,0,0};
     end
  else
     begin
      Y[1]=0;
      Y[0]=0;
      Y[2]=1;  //{0,0,1};
     end

end
endmodule

2). 不適用於DE2-70 的8bit 比較器的設計
module comp_8bit(
input [7:0]A,
input [7:0]B,
output reg [2:0]Y 
);


always@(A,B)
begin
  if (A==B)
     begin
      Y[0]=0;
      Y[1]=1;
      Y[2]=0;  //{0,1,0};
     end       
  else if (A>B)
     begin
      Y[1]=0;
      Y[0]=1;
      Y[2]=0;  //{1,0,0};
     end  
  else
     begin
      Y[1]=0;
      Y[0]=0;
      Y[2]=1;  //{0,0,1};
     end  

end
endmodule
         

3). 測試程式test bench

`timescale 1ns/1ps
module test_bench1;

reg [7:0]A;
reg [7:0]B;
wire [7:0]Y ;

comp_8bit DUT 
(.A(A),.B(B),.Y(Y));

initial 

begin
A=0; B=10;
end
always #50 A=A+1;
always #50 B=B-1;


endmodule









Node-Red --> MQTT --> Fuxa

Node-Red --> MQTT --> Fuxa      FUXA(一個開源的 Web HMI / SCADA 自動化監控軟體)的專案設定檔 。 這份設定檔完整定義了 HMI 監控畫面的 後端通訊(MQTT 連線、點位標籤) 與 前端網頁圖形介面(SVG 畫布...