⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 l07.html

📁 multiplier and divider verilog codes
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "+//Silmaril//DTD HTML Pro v0r11 19970101//EN"><html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<!-- Created by htmlize-0.49 in font mode. -->

  
    <title>l07.v</title>
  </head><body text="#000000" bgcolor="#ffffff">
    <pre><font size="+0" color="#b22222">/// </font><font size="+2" color="#000000" face="Arial,Helvetica"><b>LSU EE 3755 -- Spring 2002 -- Computer Organization</b></font><font size="+0" color="#b22222">
//
/// </font><font size="+2" color="#000000" face="Arial,Helvetica"><b>Verilog Notes 7 -- Integer Multiply and Divide</b></font><font size="+0" color="#b22222">
</font>
<font size="+0" color="#b22222">// Time-stamp: &lt;13 March 2002, 10:18:27 CST, koppel</font><font size="+0" color="#b22222">@sol&gt;</font><font size="+0" color="#b22222">
</font>
<font size="+0" color="#b22222">/// </font><font size="+2" color="#000000" face="Arial,Helvetica"><b>Contents</b></font><font size="+0" color="#b22222">
//
// Binary Multiplication Algorithm
// Simple Multiplication Hardware
// Streamlined Multiplication Hardware
// Booth Recoding for Higher-Radix and Signed Multiplication
// Binary Division Algorithm
// Division Hardware (Simple and Streamlined)
// "Real" Arithmetic Circuits
</font>

<font size="+0" color="#b22222">/// </font><font size="+2" color="#000000" face="Arial,Helvetica"><b>References</b></font><font size="+0" color="#b22222">
//
// :P:   Palnitkar, "Verilog HDL"
// :Q:   <a href="http://www.ece.lsu.edu/v/refcard.pdf">Qualis, "Verilog HDL Quick Reference Card Revision 1.0"</a>
// :H:   <a href="http://www.eg.bucknell.edu/%7Ecs320/1995-fall/verilog-manual.html">Hyde, "Handbook on Verilog HDL"</a>
// :LRM: <a href="http://www-ee.eng.hawaii.edu/%7Emsmith/ASICs/HTML/Verilog/Verilog.htm">IEEE, Verilog Language Reference Manual  (Hawaii Section Numbering)</a>
// :PH:  Patterson &amp; Hennessy, "Computer Organization &amp; Design"
// :HP:  Hennessy &amp; Patterson, "Computer Architecture: A Quantitative Approach"
</font>

<font size="+0" color="#b22222">////////////////////////////////////////////////////////////////////////////////
/// </font><font size="+2" color="#000000" face="Arial,Helvetica"><b>Binary Multiplication Algorithm</b></font><font size="+0" color="#b22222">
</font>
<font size="+0" color="#b22222">// :PH: 4.6
</font>
 <font size="+0" color="#b22222">/// </font><font size="+0" color="#b22222" face="Arial,Helvetica"><b>Long Hand Procedure Review</b></font><font size="+0" color="#b22222">
//
//  Multiply 5 times 12 in binary:
//
//     0101  Multiplicand
//     1100  Multiplier
//     """"
//     0000
//    0000
//   0101
//  0101
//  """""""
//  0111100  Product
</font>

<font size="+0" color="#b22222">////////////////////////////////////////////////////////////////////////////////
/// </font><font size="+2" color="#000000" face="Arial,Helvetica"><b>Simple Multiplication Hardware</b></font><font size="+0" color="#b22222">
</font>
<font size="+0" color="#b22222">// :PH: 4.6
</font>
 <font size="+0" color="#b22222">/// </font><font size="+0" color="#b22222" face="Arial,Helvetica"><b>Simple Multipliers</b></font><font size="+0" color="#b22222">
//
// Designed to show basic techniques, but are inefficient.
// Following sections show improved multipliers.
</font>
 <font size="+0" color="#b22222">/// </font><font size="+0" color="#b22222" face="Arial,Helvetica"><b>Multipliers in this section:</b></font><font size="+0" color="#b22222">
//
// Simple Unsigned Combinational Multiplier
//   See :PH: Figure 4.25
//   Multiplies using combinational logic only.
//   Cost of n-bit multiplier: proportional to n^2.
//   Speed of n-bit multiplier: time needed for n additions:
//      Proportional to n.
//
// Simple Unsigned Multiplier
//   Unlike combinational multiplier, uses a clock.
//   Cost of n-bit multiplier: 
//      Proportional to n.
//      Includes 5n bits of register storage.
//   Speed of n-bit multiplier: 
//      Product in n clocks.
//      Clock period includes 2n-bit add.
//   Uses more hardware than streamlined multipliers in next section.
</font>

<font size="+0" color="#b22222">// </font><font size="+0" color="#999999">:</font><font size="+0" color="#228b22"><b>Example</b></font><font size="+0" color="#999999">:</font><font size="+0" color="#b22222">
//
// Simple Unsigned Combinational Multiplier
// Based on :PH: Figure 4.25
//
// This is a straightforward coding of the longhand multiplication
// technique.
</font>
<font size="+0" color="#000080">module</font> <font size="+1" color="#000000" face="Arial,Helvetica"><b>simple_combinational_mult</b></font>(product,multiplier,multiplicand); <font size="+0" color="#b22222">//P
</font>   <font size="+0" color="#4169e1">input</font> [15:0]  <font size="+0" color="#b8860b">multiplier</font>, <font size="+0" color="#b8860b">multiplicand</font>;
   <font size="+0" color="#4169e1">output</font>        <font size="+0" color="#b8860b">product</font>;

   <font size="+0" color="#a020f0">reg</font> [31:0]    <font size="+0" color="#b8860b">product</font>;

   <font size="+0" color="#a020f0">integer</font>       <font size="+0" color="#b8860b">i</font>;

   <font size="+0" color="#000080">always</font> <font size="+0" color="#da70d6">@</font>( multiplier or multiplicand )
     <font size="+0" color="#0000cd">begin</font>
        
        product = 0;
            
        <font size="+0" color="#4169e1">for</font>(i=0; i&lt;16; i=i+1)
          <font size="+0" color="#4169e1">if</font>( multiplier[i] == <font size="+0" color="#999999">1'b</font>1 ) product = product + ( multiplicand &lt;&lt; i );
  
     <font size="+0" color="#0000cd">end</font>
     
<font size="+0" color="#000080">endmodule</font>


<font size="+0" color="#b22222">// </font><font size="+0" color="#999999">:</font><font size="+0" color="#228b22"><b>Example</b></font><font size="+0" color="#999999">:</font><font size="+0" color="#b22222">
//
// Simple Unsigned Multiplier
//
// Sequential multiplier. Handshaking signals are added to command
// multiplier to start and to indicate when finished.  Registers are
// provided for shifted versions of the multiplier and multiplicand.
</font>
<font size="+0" color="#000080">module</font> <font size="+1" color="#000000" face="Arial,Helvetica"><b>simple_mult</b></font>(product,ready,multiplier,multiplicand,start,clk); <font size="+0" color="#b22222">//P
</font>   <font size="+0" color="#4169e1">input</font> [15:0]  <font size="+0" color="#b8860b">multiplier</font>, <font size="+0" color="#b8860b">multiplicand</font>;
   <font size="+0" color="#4169e1">input</font>         <font size="+0" color="#b8860b">start</font>, <font size="+0" color="#b8860b">clk</font>;
   <font size="+0" color="#4169e1">output</font>        <font size="+0" color="#b8860b">product</font>;
   <font size="+0" color="#4169e1">output</font>        <font size="+0" color="#b8860b">ready</font>;

   <font size="+0" color="#a020f0">reg</font> [31:0]    <font size="+0" color="#b8860b">product</font>;

   <font size="+0" color="#a020f0">reg</font> [15:0]    <font size="+0" color="#b8860b">multiplier_copy</font>;
   <font size="+0" color="#a020f0">reg</font> [31:0]    <font size="+0" color="#b8860b">multiplicand_copy</font>;
   
   <font size="+0" color="#a020f0">reg</font> [4:0]     <font size="+0" color="#b8860b">bit</font>; 
   <font size="+0" color="#a020f0">wire</font>          <font size="+0" color="#000000"><b>ready </b></font>= !bit;
   
   <font size="+0" color="#000080">initial</font> bit = 0;

   <font size="+0" color="#000080">always</font> <font size="+0" color="#da70d6">@</font>( <font size="+0" color="#9400d3">posedge</font> clk )

     <font size="+0" color="#4169e1">if</font>( ready &amp;&amp; start ) <font size="+0" color="#0000cd">begin</font>

        bit               = 16;
        product           = 0;
        multiplicand_copy = { <font size="+0" color="#999999">16'd</font>0, multiplicand };
        multiplier_copy   = multiplier;
        
     <font size="+0" color="#0000cd">end</font> <font size="+0" color="#4169e1">else</font> <font size="+0" color="#4169e1">if</font>( bit ) <font size="+0" color="#0000cd">begin</font>

        <font size="+0" color="#4169e1">if</font>( multiplier_copy[0] == <font size="+0" color="#999999">1'b</font>1 ) product = product + multiplicand_copy;

        multiplier_copy = multiplier_copy &gt;&gt; 1;
        multiplicand_copy = multiplicand_copy &lt;&lt; 1;
        bit = bit - 1;

     <font size="+0" color="#0000cd">end</font>

<font size="+0" color="#000080">endmodule</font>


<font size="+0" color="#b22222">////////////////////////////////////////////////////////////////////////////////
/// </font><font size="+2" color="#000000" face="Arial,Helvetica"><b>Streamlined Multiplication Hardware</b></font><font size="+0" color="#b22222">
</font>
<font size="+0" color="#b22222">// :PH: 4.6
</font>
 <font size="+0" color="#b22222">/// </font><font size="+0" color="#b22222" face="Arial,Helvetica"><b>Streamlined Multipliers</b></font><font size="+0" color="#b22222">
//
// Uses less hardware than simple multiplier.
// Much slower than "real" multipliers.
</font>
 <font size="+0" color="#b22222">/// </font><font size="+0" color="#b22222" face="Arial,Helvetica"><b>Multipliers in this section:</b></font><font size="+0" color="#b22222">
//
// Streamlined Unsigned Multiplier
//   See :PH: Figure 4.31
//   Less register storage than simple multiplier.
//   Cost of n-bit multiplier: 
//      Proportional to n.
//      Includes 2n bits of register storage.
//   Speed of n-bit multiplier: (Same as simple.)
//      Product in n clocks.
//      Clock period includes a 2n-bit add.
//
// Streamlined Signed Multiplier
//   Like previous multiplier, but handles signed numbers.
//   A better signed multiplier covered later (using Booth recoding).
//   Cost of n-bit multiplier: 
//      Proportional to n.
//      Includes 3n bits of register storage.
//   Speed of n-bit multiplier:
//      Product in n clocks.
//      Clock period includes 2n-bit add.
</font>

<font size="+0" color="#b22222">// </font><font size="+0" color="#999999">:</font><font size="+0" color="#228b22"><b>Example</b></font><font size="+0" color="#999999">:</font><font size="+0" color="#b22222">
//
// Streamlined Unsigned Multiplier
// Based on :PH: Figure 4.31
//
// Improvements:
//
//   The product register also holds the multiplier.
//   Only the product register is shifted.
//   A register is not needed to hold the multiplicand.
//   Wow, you save 2n bits! (To multiply n-bit numbers.)
</font>
<font size="+0" color="#000080">module</font> <font size="+1" color="#000000" face="Arial,Helvetica"><b>streamlined_mult</b></font>(product,ready,multiplier,multiplicand,start,clk);
   <font size="+0" color="#4169e1">input</font> [15:0]  <font size="+0" color="#b8860b">multiplier</font>, <font size="+0" color="#b8860b">multiplicand</font>;
   <font size="+0" color="#4169e1">input</font>         <font size="+0" color="#b8860b">start</font>, <font size="+0" color="#b8860b">clk</font>;
   <font size="+0" color="#4169e1">output</font>        <font size="+0" color="#b8860b">product</font>;
   <font size="+0" color="#4169e1">output</font>        <font size="+0" color="#b8860b">ready</font>;

   <font size="+0" color="#a020f0">reg</font> [31:0]    <font size="+0" color="#b8860b">product</font>;

   <font size="+0" color="#a020f0">reg</font> [4:0]     <font size="+0" color="#b8860b">bit</font>; 
   <font size="+0" color="#a020f0">wire</font>          <font size="+0" color="#000000"><b>ready </b></font>= !bit;
   
   <font size="+0" color="#000080">initial</font> bit = 0;

   <font size="+0" color="#000080">always</font> <font size="+0" color="#da70d6">@</font>( <font size="+0" color="#9400d3">posedge</font> clk )

     <font size="+0" color="#4169e1">if</font>( ready &amp;&amp; start ) <font size="+0" color="#0000cd">begin</font>

        bit     = 16;
        product = { <font size="+0" color="#999999">16'd</font>0, multiplier };
        
     <font size="+0" color="#0000cd">end</font> <font size="+0" color="#4169e1">else</font> <font size="+0" color="#4169e1">if</font>( bit ) <font size="+0" color="#0000cd">begin</font>:<font size="+0" color="#3cb371">A</font>
        
        <font size="+0" color="#a020f0">reg</font> <font size="+0" color="#b8860b">lsb</font>;

        lsb     = product[0];
        product = product &gt;&gt; 1;
        bit     = bit - 1;

        <font size="+0" color="#4169e1">if</font>( lsb ) product[31:15] = product[30:15] + multiplicand;

     <font size="+0" color="#0000cd">end</font>

<font size="+0" color="#000080">endmodule</font>


<font size="+0" color="#b22222">// </font><font size="+0" color="#999999">:</font><font size="+0" color="#228b22"><b>Example</b></font><font size="+0" color="#999999">:</font><font size="+0" color="#b22222">
//
// Streamlined Signed Multiplier
//
// Multiplies absolute value of multiplier and multiplicand.
// Negates product if multiplier and multiplicand differ in sign.
// Otherwise similar to streamlined unsigned multiplier.
</font>
<font size="+0" color="#000080">module</font> <font size="+1" color="#000000" face="Arial,Helvetica"><b>streamlined_signed_mult</b></font>(product,ready,multiplier,multiplicand,
                               start,clk);

   <font size="+0" color="#4169e1">input</font> [15:0]  <font size="+0" color="#b8860b">multiplier</font>, <font size="+0" color="#b8860b">multiplicand</font>;
   <font size="+0" color="#4169e1">input</font>         <font size="+0" color="#b8860b">start</font>, <font size="+0" color="#b8860b">clk</font>;
   <font size="+0" color="#4169e1">output</font>        <font size="+0" color="#b8860b">product</font>;
   <font size="+0" color="#4169e1">output</font>        <font size="+0" color="#b8860b">ready</font>;

   <font size="+0" color="#a020f0">reg</font> [31:0]    <font size="+0" color="#b8860b">product</font>;
   <font size="+0" color="#a020f0">reg</font> [15:0]    <font size="+0" color="#b8860b">abs_multiplicand</font>;

   <font size="+0" color="#a020f0">reg</font> [4:0]     <font size="+0" color="#b8860b">bit</font>; 
   <font size="+0" color="#a020f0">wire</font>          <font size="+0" color="#000000"><b>ready </b></font>= !bit;
   
   <font size="+0" color="#000080">initial</font> bit = 0;

   <font size="+0" color="#000080">always</font> <font size="+0" color="#da70d6">@</font>( <font size="+0" color="#9400d3">posedge</font> clk )

     <font size="+0" color="#4169e1">if</font>( ready &amp;&amp; start ) <font size="+0" color="#0000cd">begin</font>

        bit     = 16;
        product = { <font size="+0" color="#999999">16'd</font>0, multiplier[15] ? -multiplier : multiplier };
        abs_multiplicand = multiplicand[15] ? -multiplicand : multiplicand;
        
     <font size="+0" color="#0000cd">end</font> <font size="+0" color="#4169e1">else</font> <font size="+0" color="#4169e1">if</font>( bit ) <font size="+0" color="#0000cd">begin</font>:<font size="+0" color="#3cb371">A</font>
        
        <font size="+0" color="#a020f0">reg</font> <font size="+0" color="#b8860b">lsb</font>;

        lsb     = product[0];
        product = product &gt;&gt; 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -