1

关于投票
时序Case和组合Case资源比较

同学问我一道O2的笔试题,

module   C_case1(clk,out1);
input  clk;
output out1;

reg  state1;
reg  out1;


always @(posedge clk)
  case(state1)
   0: state1<=1;
 1: state1<=0;
  endcase
always @(state1)
  case(state1)
   0: out1<=1;
 1: out1<=0;
 endcase
endmodule

 

 module   C_case2(clk,out2);
input  clk;
output out2;

reg  state2;
reg  out2;
 
 always @(posedge clk)

  case(state2)
   0:begin
 state2<=1;
   out2<=1;
   end
 1:begin
 state2<=0;
   out2<=0;
   end
   endcase
 
 
 endmodule

 

两者资源比较。

在Quartus综合结果如下:

第一种:

点击看大图

组合Case

第二种:

点击看大图

时序Case

 

明显第一种比第二种省个触发器

工程文件打包如下:

rar

系统分类: CPLD/FPGA
用户分类: Verilog
标签: 无标签
来源: 原创
发表评论 阅读全文(583) | 回复(2)

1

关于投票
[ZZ]RS232发送的代码
verilog 中 数据发送的描述和应用!, 本人的实战代码~


这是本人用FPGA做RS232发送的代码一部分:
目的是想将一下数据发送的verilog代码怎么样描述比较贴近底层
和高效率的运行,虽然没有用上经典FPGA原则:(乒乓操作,PIPLING)
但是有一定的借鉴作用:


一般的RS232的通信是9600bps 10 bit ——1 bit begin ,1 bit end ,and
8bit data ;
if we use 11.0592MHZ(便于和51单片机通信)
我们将分频 11.0592M/9600=1152

分频部分省略:
这里只说主要参数和设计原则,
output out;
reg start_flag; // start_flag
reg [7:0] uart_buf; // send buffer;
reg txd_reg ;// txd register;
reg [3:0] bit_count // bit which send counter

always@(posedge clock) //clock 为分频后的时钟!
begin
if(start_flag||bit_count<4'd10)//因为我们要保证发送的数据只有10位
bit_count=bit_count+1;//而且如果没有发送完要继续发送
else bit_count=0; //如果要加入可调 波特率,可以自己设计

case(bit_count)
4'h0:txd_reg= 0;
4'h1:txd_reg= uart_buf[0]
4'h2:txd_reg= uart_buf[1];
4'h3:txd_reg= uart_buf[2];
4'h4:txd_reg= uart_buf[3];
4'h5:txd_reg= uart_buf[4]
4'h6:txd_reg= uart_buf[5];
4'h7:txd_reg= uart_buf[6];
4'h8:txd_reg= uart_buf[7];
4'h9:txd_reg= 1;
default: txd_reg=1;
endcase
end

assign out="txd"_reg;

好了,这是代码,我来说一下设计思想,
对于要用到数据传输的系统中,这种设计思想是必备的;
比如有串数据在buffer[20]中,用C语言来写
可能是这样的:
for(i=0;i<20;i++)
send=buffer[i];

但是这种写法在硬件描述语言中虽然是正确的但不符合
做FPGA的原则:面积与速度;这段代码在这两个方面上
都没有符合要求;
所以

我们要有一个硬件的设计思想
就是设计一个计数器COUNTER这个计数器,的个数是你要发送的数据个数
也可以设计成可调的,
然后用CASE。。。。。ENDCASE语句来扑捉这个COUNTER
对于不同COUNTER 发送不同的数据位,输出可以是并行的也可以是串行的!
如果加上乒乓操作,数据传输的将更快,再加上IP复用面积就更小了!
好了,本人愚作,谢谢欣赏!
系统分类: CPLD/FPGA
用户分类: Verilog
标签: 无标签
来源: 转贴
发表评论 阅读全文(734) | 回复(1)

2

关于投票
一些简单的Verilog代码

一、

//Simple module that connect sthe SW switches to the LEDR lights
module part1(SW,LEDR);
  input [17:0] SW;//toggleswitches
output[17:0]LEDR;//redLEDs
assignLEDR=SW;
endmodule

二、

点击看大图

// Implements eight 2-to-1 multiplexers.
// inputs:  SW7-0 represent the 8-bit input X, and SW15-8 represent Y
//            SW17 selects either X or Y to drive the output LEDs
// outputs:  LEDR17-0 show the states of the switches
//               LEDG7-0 shows the outputs of the multiplexers
module part2 (SW, LEDR, LEDG);
   input [17:0] SW;         // toggle switches
   output [17:0] LEDR;   // red LEDs
   output [7:0] LEDG;    // green LEDs

   wire Sel;
   wire [7:0] X, Y, M;

   assign  LEDR = SW;
   assign  X        = SW[7:0];
   assign  Y        = SW[15:8];
   assign  Sel      = SW[17];

   assign M[0] = (~Sel & X[0]) | (Sel & Y[0]);
   assign M[1] = (~Sel & X[1]) | (Sel & Y[1]);
   assign M[2] = (~Sel & X[2]) | (Sel & Y[2]);
   assign M[3] = (~Sel & X[3]) | (Sel & Y[3]);
   assign M[4] = (~Sel & X[4]) | (Sel & Y[4]);
   assign M[5] = (~Sel & X[5]) | (Sel & Y[5]);
   assign M[6] = (~Sel & X[6]) | (Sel & Y[6]);
   assign M[7] = (~Sel & X[7]) | (Sel & Y[7]);
   assign LEDG[7:0] = M;
endmodule

三、

点击看大图点击看大图

// Implements a 3-bit wide 5-to-1 multiplexer.
// inputs: SW14-0 represent data in 5 groups, U-Y
//           SW17-15 selects one group from U to Y
// outputs: LEDR17-0 show the states of the switches
//             LEDG2-0 displays the selected group
module part3 (SW, LEDR, LEDG);
   input [17:0] SW;    // toggle switches
   output [17:0] LEDR;   // red LEDs
   output [2:0] LEDG;   // green LEDs

   wire [1:3] m_0, m_1, m_2; // m_0 is used for 3 intermediate multiplexers 
                                             // to produce the 5-to-1 multiplexer M[0], m_1 is for
                                            // M[1], and m_2 is for M[2]
   wire [2:0] S, U, V, W, X, Y, M; // M is the 3-bit 5-to-1 multiplexer

   assign S[2:0] = SW[17:15];
   assign U = SW[2:0];
   assign V = SW[5:3];
   assign W = SW[8:6];
   assign X = SW[11:9];
   assign Y = SW[14:12];

   assign LEDR = SW;

   // 5-to-1 multiplexer for bit 0
   assign m_0[1] = (~S[0] & U[0]) | (S[0] & V[0]);
   assign m_0[2] = (~S[0] & W[0]) | (S[0] & X[0]);
   assign m_0[3] = (~S[1] & m_0[1]) | (S[1] & m_0[2]);
   assign M[0] = (~S[2] & m_0[3]) | (S[2] & Y[0]); // 5-to-1 multiplexer output

   // 5-to-1 multiplexer for bit 1
   assign m_1[1] = (~S[0] & U[1]) | (S[0] & V[1]);
   assign m_1[2] = (~S[0] & W[1]) | (S[0] & X[1]);
   assign m_1[3] = (~S[1] & m_1[1]) | (S[1] & m_1[2]);
   assign M[1] = (~S[2] & m_1[3]) | (S[2] & Y[1]); // 5-to-1 multiplexer output
 
  // 5-to-1 multiplexer for bit 2
   assign m_2[1] = (~S[0] & U[2]) | (S[0] & V[2]);
   assign m_2[2] = (~S[0] & W[2]) | (S[0] & X[2]);
   assign m_2[3] = (~S[1] & m_2[1]) | (S[1] & m_2[2]);
   assign M[2] = (~S[2] & m_2[3]) | (S[2] & Y[2]); // 5-to-1 multiplexer output
 
   assign LEDG[2:0] = M;
endmodule

四、

点击看大图

// Implements a circuit that can display five characters on a 7-segment
// display.
// inputs: SW2-0 selects the letter to display. The characters are:
//  SW 2 1 0  Char
//  ----------------
//     0 0 0   'H'
//   0 0 1  'E'
//   0 1 0  'L'
//   0 1 1  'O'
//   1 0 0  ' ' Blank
//   1 0 1  ' ' Blank
//   1 1 0  ' ' Blank
//   1 1 1  ' ' Blank
//
// outputs: LEDR2-0 show the states of the switches
//    HEX0 displays the selected character
module part4 (SW, LEDR, HEX0);
   input [2:0] SW;    // toggle switches
   output [2:0] LEDR;   // red LEDs
   output [0:6] HEX0;   // 7-seg display

   wire [2:0] C;

   assign LEDR = SW;
   assign C[2:0] = SW[2:0];

 /*
  *       0 
  *      --- 
  *     |   |
  *    5|   |1
  *     | 6 |
  *      --- 
  *     |   |
  *    4|   |2
  *     |   |
  *      --- 
  *       3 
  */
 // the following equations describe HEX0[0-6] in cannonical SOP form
   assign HEX0[0] = ~((~C[2] & ~C[1] & C[0]) | (~C[2] & C[1] & C[0]));
   assign HEX0[1] = ~((~C[2] & ~C[1] & ~C[0]) | (~C[2] & C[1] & C[0]));
   assign HEX0[2] = ~((~C[2] & ~C[1] & ~C[0]) | (~C[2] & C[1] & C[0]));
   assign HEX0[3] = ~((~C[2] & ~C[1] & C[0]) | (~C[2] & C[1] & ~C[0]) |
                               (~C[2] & C[1] & C[0]));
   assign HEX0[4] = ~((~C[2] & ~C[1] & ~C[0]) | (~C[2] & ~C[1] & C[0]) |
                               (~C[2] & C[1] & ~C[0]) | (~C[2] & C[1] & C[0]));
   assign HEX0[5] = ~((~C[2] & ~C[1] & ~C[0]) | (~C[2] & ~C[1] & C[0]) |
                               (~C[2] & C[1] & ~C[0]) | (~C[2] & C[1] & C[0]));
   assign HEX0[6] = ~((~C[2] & ~C[1] & ~C[0]) | (~C[2] & ~C[1] & C[0]));
endmodule

 

 

 

系统分类: CPLD/FPGA
用户分类: Verilog
标签: 无标签
来源: 原创
发表评论 阅读全文(809) | 回复(1)
总共 , 当前 /