<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2022-05-28T09:21:18+00:00</updated><id>/feed.xml</id><title type="html">Dr Matthew (Wai-Chung) Tang 鄧偉宗博士</title><subtitle>Lecturer; Embedded System Engineer; Love anything about electronics</subtitle><author><name>Matthew Tang</name></author><entry><title type="html">Xilinx Vivado (Webpack) 2017.3 Installation</title><link href="/general/2022/05/28/vivado-install.html" rel="alternate" type="text/html" title="Xilinx Vivado (Webpack) 2017.3 Installation" /><published>2022-05-28T09:02:00+00:00</published><updated>2022-05-28T09:02:00+00:00</updated><id>/general/2022/05/28/vivado-install</id><content type="html" xml:base="/general/2022/05/28/vivado-install.html">&lt;p&gt;In my Digital System Design module in &lt;a href=&quot;http://www.eecs.qmul.ac.uk&quot;&gt;QMUL EECS&lt;/a&gt;, 
we used the &lt;a href=&quot;https://www.ni.com/docs/en-US/bundle/digital-systems-development-specs/resource/376641b.pdf&quot;&gt;NI DSDB board&lt;/a&gt;
for lab exercises. The board is very powerful and can be programmed via Xilinx
Vivado or NI Labview. In this blog of reminder, I am going to outline the
installation process for Xilinx Vivado version &lt;a href=&quot;https://www.xilinx.com/support/download/index.html/content/xilinx/en/downloadNav/vivado-design-tools/archive.html&quot;&gt;2017.3&lt;/a&gt; which gives the best
compatibilty for the DSDB board.&lt;/p&gt;

&lt;p&gt;First, download the 
&lt;strong&gt;Vivado HLx 2017.3: WebPACK and Editions - Windows Self Extracting Web Installer&lt;/strong&gt;
from the Xilinx website (link above).&lt;/p&gt;

&lt;p&gt;Then start the installation by opening the file. Skip the latest version of
course by clicking &lt;strong&gt;Continue&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/VivadoInstall/vivado2017_3-1.png&quot; alt=&quot;1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you have not yet got a Xilinx account, follow the links to create one. Enter
your credentials here and carry on with &lt;strong&gt;Download and Install Now&lt;/strong&gt;.
&lt;img src=&quot;/images/VivadoInstall/vivado2017_3-2.png&quot; alt=&quot;2&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Agree the terms of licenses …&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/VivadoInstall/vivado2017_3-3.png&quot; alt=&quot;3&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Choose the free &lt;strong&gt;Vivado HL WebPack&lt;/strong&gt; version …&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/VivadoInstall/vivado2017_3-4.png&quot; alt=&quot;4&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Double check the installation locations and so on.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/VivadoInstall/vivado2017_3-5.png&quot; alt=&quot;5&quot; /&gt;
&lt;img src=&quot;/images/VivadoInstall/vivado2017_3-6.png&quot; alt=&quot;6&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Wait patiently till the installation finishes …&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/VivadoInstall/vivado2017_3-7.png&quot; alt=&quot;7&quot; /&gt;&lt;/p&gt;

&lt;p&gt;During the process, you will be prompted to install some Windows drivers for
debugging cables.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/VivadoInstall/vivado2017_3-8.png&quot; alt=&quot;8&quot; /&gt;&lt;/p&gt;

&lt;p&gt;That should work till the end of the installation.
Next time, I will share an example of a simple design that demonstrate the basic
input and output devices available on the DSDB board.&lt;/p&gt;</content><author><name>Matthew Tang</name></author><category term="General" /><summary type="html">In my Digital System Design module in QMUL EECS, we used the NI DSDB board for lab exercises. The board is very powerful and can be programmed via Xilinx Vivado or NI Labview. In this blog of reminder, I am going to outline the installation process for Xilinx Vivado version 2017.3 which gives the best compatibilty for the DSDB board.</summary></entry><entry><title type="html">Keil uVision in Virtual Machine</title><link href="/general/2022/05/02/Keil-VM.html" rel="alternate" type="text/html" title="Keil uVision in Virtual Machine" /><published>2022-05-02T13:14:00+00:00</published><updated>2022-05-02T13:14:00+00:00</updated><id>/general/2022/05/02/Keil-VM</id><content type="html" xml:base="/general/2022/05/02/Keil-VM.html">&lt;p&gt;This is a note to myself and my students about installing Keil uVision via
Virtual Machines.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;1-virtual-box&quot;&gt;1. Virtual Box&lt;/h2&gt;

&lt;p&gt;This &lt;a href=&quot;https://gab.wallawalla.edu/~larry.aamodt/cptr215/using_keil_w_vbox.pdf&quot;&gt;instruction&lt;/a&gt;
is intended for Ubuntu Linux, but the steps should be similar for MacOS&lt;/p&gt;

&lt;h2 id=&quot;2a-windows-xp&quot;&gt;2a. Windows XP&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://fweb.wallawalla.edu/~frohro/ClassHandouts/Assembly/cptr215.ova&quot;&gt;Link to VM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On WinXP, download and install &lt;a href=&quot;http://www2.keil.com/mdk5/legacy&quot;&gt;&lt;strong&gt;v5.18&lt;/strong&gt;&lt;/a&gt; instead of latest version.&lt;/p&gt;

&lt;h2 id=&quot;2b-windows-10-slower-on-most-machines&quot;&gt;2b. Windows 10 (Slower on most machines)&lt;/h2&gt;

&lt;p&gt;The Keil ARM-MDK only supports Windows OS. For Linux/Mac users, the only way for running that for our lab exercises is to use a Virtual Machine (VM). The following steps will help you set up a free Windows 10 (Win10) VM using VirtualBox and run Keil uVision inside.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Download and install an official Microsoft Windows 10 Virtual Machine:
&lt;a href=&quot;https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/&quot;&gt;Windows 10 with Legacy Microsoft Edge and Internet Explorer 11 (6.7 GB)&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Install &lt;a href=&quot;https://www.virtualbox.org/&quot;&gt;VirtualBox&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Unzip the file from step 1 and import that in VirtualBox.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Start the Win10. Transfer the Keil installer (+ software pack) to the VM (e.g. Drag-n-Drop). Install Keil uVision and software pack inside the VM.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Test running Keil uVision with our lab files.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;</content><author><name>Matthew Tang</name></author><category term="General" /><summary type="html">This is a note to myself and my students about installing Keil uVision via Virtual Machines.</summary></entry><entry><title type="html">Quartus Lite 18.1 Installation</title><link href="/general/2021/12/17/quartus-Install.html" rel="alternate" type="text/html" title="Quartus Lite 18.1 Installation" /><published>2021-12-17T15:55:00+00:00</published><updated>2021-12-17T15:55:00+00:00</updated><id>/general/2021/12/17/quartus-Install</id><content type="html" xml:base="/general/2021/12/17/quartus-Install.html">&lt;p&gt;In one of my modules, I used the &lt;a href=&quot;https://www.terasic.com.tw/cgi-bin/page/archive.pl?Language=English&amp;amp;CategoryNo=234&amp;amp;No=1021&quot;&gt;Terasic DE10-Lite FPGA board&lt;/a&gt; for labs. 
This handy, little board can be programmed easily via a USB-A cable as it has
the Altera USB-Blaster programmer already. My students found it very useful to
verify their digital designs on board.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.mouser.co.uk/images/marketingid/2016/microsites/132318264/terasic-de10-lite-board-layout.jpg&quot; alt=&quot;DE10-Lite From Mouser.com&quot; title=&quot;DE10-Lite image from mouser.com&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this article, I will list the main steps to install the 
&lt;a href=&quot;https://fpgasoftware.intel.com/?edition=lite&quot;&gt;Intel Quartus Lite&lt;/a&gt; design software 
(version 18.1 that DE10-Lite originally asks for, despite newer versions are
known to work well) with ModelSim simulation tool and MAX-10 FPGA support.&lt;/p&gt;

&lt;p&gt;First, download the following from the Intel website (link above):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QuartusLiteSetup-18.1.0.625-windows.exe&lt;/code&gt; (version may change)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ModelSimSetup-18.1.0.625-windows.exe&lt;/code&gt; (version should match with Quartus Lite)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max10-18.1.0625.qdz&lt;/code&gt; (MAX-10 FPGA support files)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;/images/QuartusInstall/1-Files_required.png&quot; alt=&quot;Files Required&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Then start the installation by opening
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QuartusLiteSetup-18.1.0.625-windows.exe&lt;/code&gt;. Choose the minimal set of components
to work with the DE10-Lite board, as shown.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/QuartusInstall/2-Install_options.png&quot; alt=&quot;2&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Wait patiently till the installation finishes …
&lt;img src=&quot;/images/QuartusInstall/3-Wait_toComplete.png&quot; alt=&quot;3&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Next, check the option to &lt;strong&gt;launch USB Blaster II driver installation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/QuartusInstall/4-Completed.png&quot; alt=&quot;4&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Accept the security warning and check the screen for a successful notice.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/QuartusInstall/5-Drivers.png&quot; alt=&quot;5&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/QuartusInstall/6-Drivers_done.png&quot; alt=&quot;6&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note: you may still need to manual locate the driver when the board is plugged
in the first time to your PC/laptop. Check &lt;a href=&quot;https://www.terasic.com.tw/wiki/Altera_USB_Blaster_Driver_Installation_Instructions&quot;&gt;this link&lt;/a&gt; for steps.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When you run the software for the first time, you will be asked to buy a full
license. We will skip and run the basic lite version.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/QuartusInstall/7-Run_first_time.png&quot; alt=&quot;7&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The installation is now complete. The design software is ready to compile
VHDL/Verilog designs and program them onto the FPGA boards.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/QuartusInstall/8-Quartus_Window.png&quot; alt=&quot;8&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Next time, I will share an example of a simple design that demonstrate the basic
input and output devices available on the board.&lt;/p&gt;</content><author><name>Matthew Tang</name></author><category term="General" /><summary type="html">In one of my modules, I used the Terasic DE10-Lite FPGA board for labs. This handy, little board can be programmed easily via a USB-A cable as it has the Altera USB-Blaster programmer already. My students found it very useful to verify their digital designs on board.</summary></entry><entry><title type="html">Building an extensive synchronous up/down counter with enable</title><link href="/arithmetic/2021/12/14/sync-counter.html" rel="alternate" type="text/html" title="Building an extensive synchronous up/down counter with enable" /><published>2021-12-14T10:45:00+00:00</published><updated>2021-12-14T10:45:00+00:00</updated><id>/arithmetic/2021/12/14/sync-counter</id><content type="html" xml:base="/arithmetic/2021/12/14/sync-counter.html">&lt;p&gt;This is a popular exercise that I’d love to discuss with my students. The design
is very practical and it reveals the basic idea how to generalise the ripple
adder concept to create an iterative circuit for a synchronous counter. The
requirement of having up or down control also challenges the understanding of
the 2’s complement arithmetic which is usually introduced earlier.&lt;/p&gt;

&lt;h2 id=&quot;the-question&quot;&gt;The question&lt;/h2&gt;

&lt;p&gt;We want to build a up/down synchronous counter with an enable signal. The basic
structure of the design is given as follows:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/updn_counter_begin.png&quot; alt=&quot;Up/Down Counter Basic Structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The design is iterative so that more bit slices which each contains a mystery block and a
D-type flip-flop can be added to the end (left hand side of the diagram above)
to extend the counter for more bits. But in this exercise, we focus on a 4-bit
design. \(Q\) is the output of the counter and it counts up (e.g. 0100, 0101,
0110, ..) and down (e.g. 0011, 0010, 0001, …) depending on the control input
\(up/down'\). There is also an \(Enable\) signal which starts or stops the counting
in either up or down mode.&lt;/p&gt;

&lt;p&gt;So the question is now:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;What should be the mystery (combinational) block?&lt;/li&gt;
  &lt;li&gt;How to connect the \(up/down'\) and \(enable\) to the mystery blocks to
fulfill the requirement? &lt;em&gt;You may add some logic gates but they should be kept as
few as possible.&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s work on the exercise and read on when you have your solution!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;hints--the-partial-solution&quot;&gt;Hints / the partial solution&lt;/h2&gt;

&lt;p&gt;It is not too difficult to guess that the mystery block is a full adder. 
So now there are three bits of input to consider (i.e. \(A\), \(B\), \(C_{in}\)).&lt;/p&gt;

&lt;p&gt;It is useful to write down the requirement in a table, with regards to the
control inputs of the counter.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;+----------+--------+-----------+
| up/down' | enable |     Q     |
+==========+========+===========+
|    0     |   0    | no change |
+----------+--------+-----------+
|    0     |   1    | count down|
+----------+--------+-----------+
|    1     |   0    | no change |
+----------+--------+-----------+
|    1     |   1    | count up  |
+----------+--------+-----------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Clearly \(Q\) outputs of the flip-flops go back to input \(B\). And except for the first
FA, \(C_{in}\) and \(C_{out}\) are chained together similar to a ripple adder. Therefore I
would prefer to have the same input to all As of the FAs, while a specific input
to the first \(C_{in}\) (let’s called it \(C_{in0}\) for now).&lt;/p&gt;

&lt;p&gt;Let’s rewrite the above table based on this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;+----------+--------+----------+
| up/down' | enable |    Q     |
+==========+========+==========+
|    0     |   0    |no change |
+----------+--------+----------+
|    0     |   1    | 1111 + 0 |
+----------+--------+----------+
|    1     |   0    |no change |
+----------+--------+----------+
|    1     |   1    | 0000 + 1 |
+----------+--------+----------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This means that when the counter counts up, \(A\) = 0 to all FAs and \(C_{in0}\) = 1. 
Similarly \(A\) = 1 and \(C_{in0}\) = 0 when we count down 
(1111 is the 2’s complement representation of -1, isn’t it?)&lt;/p&gt;

&lt;p&gt;You can now try to complete the table for rows 1 &amp;amp; 3. Then you should be able to
work out the additional gates and connections required (if not, you can read on
to the full solution)&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;the-last-piece-of-puzzle--the-full-solution&quot;&gt;The last piece of puzzle / the full solution&lt;/h2&gt;

&lt;p&gt;If we follow from the partial solution, the complete table would be:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;+----------+--------+----------+
| up/down' | enable | A + Cin0 |
+==========+========+==========+
|    0     |   0    | 1111 + 1 |
+----------+--------+----------+
|    0     |   1    | 1111 + 0 |
+----------+--------+----------+
|    1     |   0    | 0000 + 0 |
+----------+--------+----------+
|    1     |   1    | 0000 + 1 |
+----------+--------+----------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;where I added a trick on the first row to add 10000 = 1111 + 1 instead of 00000. The
main reason is that I would like to match the bits with the complement of the
\(up/down'\) signal. And from the truth table we can see that we need an XNOR
gate to generate \(C_{in0}\). As a conclusion:&lt;/p&gt;

\[\begin{matrix}
A &amp;amp; = (up/down')'\\
C_{in0} &amp;amp; = (up/down') \bar{\oplus} enable\\
\end{matrix}\]

&lt;p&gt;Two additional logic gates are added to complete the solution.&lt;/p&gt;

&lt;p&gt;I will be sharing the VHDL implementation in a future post.&lt;/p&gt;</content><author><name>Matthew Tang</name></author><category term="Arithmetic" /><summary type="html">This is a popular exercise that I’d love to discuss with my students. The design is very practical and it reveals the basic idea how to generalise the ripple adder concept to create an iterative circuit for a synchronous counter. The requirement of having up or down control also challenges the understanding of the 2’s complement arithmetic which is usually introduced earlier.</summary></entry><entry><title type="html">Building a digital circuit for addition</title><link href="/arithmetic/2021/01/15/digital-adder.html" rel="alternate" type="text/html" title="Building a digital circuit for addition" /><published>2021-01-15T13:38:00+00:00</published><updated>2021-01-15T13:38:00+00:00</updated><id>/arithmetic/2021/01/15/digital-adder</id><content type="html" xml:base="/arithmetic/2021/01/15/digital-adder.html">&lt;p&gt;First of all, digital circuits work with 0’s and 1’s (i.e. bits). 
Numbers are therefore represented in binary before they can be added in the
digital circuits that we are going to design. For now, let’s assume numbers are
non-negative \(X = 0, 1, 2, \ldots\) so that a simple binary conversion
suffices, for instance:&lt;/p&gt;

\[X = 9_{10} = 1001_{2}\]

\[Y = 4_{10} = 0100_{2}\]

&lt;p&gt;We add digit by digit from the least significant end (right hand side) through
to the most significant end (left hand side), with possibly a carry between
digits. If we denote each bit of number \(X\) as \(X_i\), then we need a circuit
to add bits \(X_i\) and \(Y_i\) with a carry in \(C_i\). And the circuit
produces a sum bit \(S_i\) and a carry out bit \(C_{i+1}\), which is the carry
in bit for the next digits.&lt;/p&gt;

&lt;h1 id=&quot;the-full-adder&quot;&gt;The Full Adder&lt;/h1&gt;

&lt;p&gt;The behaviour of the circuit that adds the bits can be easily listed in a truth
table:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;\(C_i\)&lt;/th&gt;
      &lt;th&gt;\(X_i\)&lt;/th&gt;
      &lt;th&gt;\(Y_i\)&lt;/th&gt;
      &lt;th&gt;\(C_{i+1}\)&lt;/th&gt;
      &lt;th&gt;\(S_i\)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;This circuit is called the &lt;strong&gt;full adder&lt;/strong&gt;. And there are several interesting ways to study 
the truth table:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The first four rows are the cases when the carry in is 0. Extracting these rows without the first column gives you the truth table of the &lt;strong&gt;half adder&lt;/strong&gt; that adds two bits and gives a sum bit and a carry bit.&lt;/li&gt;
  &lt;li&gt;The output column \(C_{i+1}\) is counting two or more 1’s while the output column \(S_i\) is counting odd number of 1’s.&lt;/li&gt;
  &lt;li&gt;\(S_i\) and \(C_{i+1}\) are two different functions of our input bits \(X_i\), \(Y_i\) and \(S_i\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Based on the second point above, we can derive simple Boolean equations for
\(S_i\) and \(C_{i+1}\):&lt;/p&gt;

\[\begin{align}
S_i &amp;amp;= AB + AC + BC\\
C_{i+1} &amp;amp;= A \oplus B \oplus C\\
\end{align}\]

&lt;p&gt;If you are not familiar with exclusive OR gate as denoted by \(\oplus\) you can
check it out &lt;a href=&quot;https://www.electrical4u.com/exclusive-or-gate/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let’s give a symbol for the full adder to help our following discussion.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/full_adder.png&quot; width=&quot;250px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The arrows indicate the flow of the input bits, carry bits and the sum bits. And
when there is no ambiguity the labels for the inputs/outputs are skipped in the
following diagrams.&lt;/p&gt;

&lt;h1 id=&quot;the-ripple-adder&quot;&gt;The Ripple Adder&lt;/h1&gt;

&lt;p&gt;The full adder is the circuit/device to handle addition of two bits so the
remaining job is to connect the carries together, forming a chain along a list
of full adders. For example, a 4-bit ripple adder is like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/ripple_adder_4.png&quot; alt=&quot;4-bit ripple adder&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The design can be easily extended for as many bits as you want. You just need to
append more full adders to the end of the chain. The more significant bits are
on the left of the diagram so it matches our convention on paper. 
We also call this design &lt;strong&gt;iterative&lt;/strong&gt; as it can be used repeatedly to construct
bigger circuits for the same operation.&lt;/p&gt;

&lt;p&gt;This gives the standard ripple adder which is used so many digital systems that
involves additions and subtractions. It is imperative to note that the
size (or width) of the adder, \(N\) has to be decided at the time when the adder is
built because the adder cannot be extended easily once it sits in the system
(unless you are using programmable logic devices like FPGAs). As a result, the
rule of thumb is to estimate the order of your calculations and make the adder
big enough. For instance, if you intend to add numbers in a thousand range then
you probably need an adder of 10-12 bits wide.&lt;/p&gt;

&lt;p&gt;When \(X\) and \(Y\) are non-negative (i.e. unsigned), a carry out from your
last full adder (e.g. \(C_4\) in the diagram above) indicates an overflow of the
\(N\)-bit adder. You can save the carry to preserve the correct sum or simply
raise an error as you cannot go on with further additions.&lt;/p&gt;

&lt;p&gt;For now, addition is sorted, but how about subtraction? In the next blog, I will
discuss the modification required to turn a ripple adder into a subtractor using
the useful 2’s complement representation.&lt;/p&gt;</content><author><name>Matthew Tang</name></author><category term="Arithmetic" /><summary type="html">First of all, digital circuits work with 0’s and 1’s (i.e. bits). Numbers are therefore represented in binary before they can be added in the digital circuits that we are going to design. For now, let’s assume numbers are non-negative \(X = 0, 1, 2, \ldots\) so that a simple binary conversion suffices, for instance:</summary></entry><entry><title type="html">A Trick for Subtraction</title><link href="/arithmetic/2020/12/16/tens-complement.html" rel="alternate" type="text/html" title="A Trick for Subtraction" /><published>2020-12-16T09:59:00+00:00</published><updated>2020-12-16T09:59:00+00:00</updated><id>/arithmetic/2020/12/16/tens-complement</id><content type="html" xml:base="/arithmetic/2020/12/16/tens-complement.html">&lt;p&gt;The first series of my blogs is to explain how we build a digital circuit for
addition and subtraction – the most fundamental arithmetic operations. I will
start with some theory of the maths and then discuss the circuitry. The
discussion is a background for us to understand the 
&lt;a href=&quot;https://wctangcse.github.io/arithmetic/2020/12/13/signed-multiplication-1.html&quot;&gt;signed multiplication&lt;/a&gt; that I wrote earlier on.&lt;/p&gt;

&lt;p&gt;To begin with, I would like to show a little trick on doing subtraction without
really doing any subtraction at all! The example is as follows: I am asked to do&lt;/p&gt;

\[5 - 3\]

&lt;p&gt;But suppose I do not know (or want) to subtract, so I rewrite the expression as&lt;/p&gt;

\[5 + (-3)\]

&lt;p&gt;Then the &lt;em&gt;negative&lt;/em&gt; of 3 is formed by taking the partner of 3 in this table, and
plus 1.&lt;/p&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;9&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;8&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;7&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;6&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Therefore, we have&lt;/p&gt;

\[5 - 3 = 5 + (-3) := 5 + (6 + 1) = \cancel{1}2\]

&lt;p&gt;By dropping the carry (based on the fact that the difference is always less than 10),
we found the difference &lt;strong&gt;2&lt;/strong&gt; without really subtracting!&lt;/p&gt;

&lt;p&gt;Let’s try again with another example:&lt;/p&gt;

\[4 - 1 = 4 + (-1) := 4 + (8 + 1) = \cancel{1}3\]

&lt;p&gt;We can extend the method for bigger numbers too:&lt;/p&gt;

\[25 - 18 = 25 + (-18) := 25 + (81 + 1) = \cancel{1}07\]

&lt;p&gt;where each of the digits of 18 had its partner looked up.&lt;/p&gt;

&lt;h1 id=&quot;why-does-it-work&quot;&gt;Why does it work?&lt;/h1&gt;

&lt;p&gt;The idea behind this trick is rather simple – we borrow a fixed but enough amount so that
the subtraction can be turned into an easy table look up (and plus one). If you
look at the table and the examples again you will notice:&lt;/p&gt;

\[\begin{aligned}
(-3) &amp;amp;:= 10 - 3 = 9 - 3 + 1 &amp;amp;= 6 + 1\\
(-1) &amp;amp;:= 10 - 1 = 9 - 1 + 1 &amp;amp;= 8 + 1\\
(-18) &amp;amp;:= 100 - 18 = 99 - 18 + 1 &amp;amp;= 81 + 1
\end{aligned}\]

&lt;p&gt;Since the table is pairing bonds of 9, the actual subtraction is replaced (or
intuitively done) by the table look up. And the addition of patching 1 is to
make 9 or 99 back to 10 or 100, such that the calculation is correct once the
carry that we are expecting is being ignored (or discarded).&lt;/p&gt;

&lt;p&gt;You will be now tempted to try an example when you do not have enough to
subtract:&lt;/p&gt;

\[3 - 4 = 3 + (-4) := 3 + (5 + 1) = 9\]

&lt;p&gt;There are two points to note: (1) the borrow of 10 when we look up the table is
not returned in the end, so we know there is not enough to subtract; (2) 9 can
be viewed as (-1) if you accept negative numbers (I will explain this further
later). Anyway we can now generalise this method.&lt;/p&gt;

&lt;h1 id=&quot;the-10s-complement-method-for-subtraction-unsigned&quot;&gt;The 10’s Complement Method for Subtraction (Unsigned)&lt;/h1&gt;

&lt;p&gt;For now, I consider only unsigned numbers (i.e. zero and natural numbers).
Given a number $X$ of $N$ decimal digits, we define the 10’s complement of
\(X\) as:&lt;/p&gt;

\[Tenscomp(X) = 10^N - X = (10^N - 1) - X + 1 = 9\ldots99 - X + 1\]

&lt;p&gt;So the 10’s complement can be found by looking up the table above for each of
the digits of \(X\) followed by adding an extra one. 
By definition, the sum of \(X\) and its 10’s complement is exactly \(10^N\). And
then the subtraction between two numbers \(X\) and \(Y\) can be expressed as:&lt;/p&gt;

\[X - Y = X + (10^N - Y) - 10^N = X + Tenscomp(Y) - 10^N\]

&lt;p&gt;When \(X\geq Y\), the sum \(X + Tenscomp(Y)\) will be greater or equal to \(10^N\) (i.e. we
can return the borrow by ignoring the carry); otherwise we can tell that \(X &amp;lt; Y\).&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example: 741 - 258&lt;/p&gt;

\[Tenscomp(258) = 741 + 1 = 742\]

\[741 - 258 := 741 + 742 = \cancel{1}483\]

  &lt;p&gt;We return \(10^3 = 1000\) in the end of the calculation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1 id=&quot;isnt-that-it-complicates-the-calculation&quot;&gt;Isn’t that it complicates the calculation?&lt;/h1&gt;

&lt;p&gt;Yes indeed, on a human perspective. But if we are to design a machine for both
addition and subtraction, then we can avoid subtraction by doing a table look up
followed by addition. Furthermore, the fewer digits you use the easier the look up
process is. In digital circuits, as you may know, we use only two digits 0 and 1. 
So the table look up is now further simplified as a digit (bit) flipping (\(0\rightarrow 1\)
or \(1\rightarrow 0\) that can be realised efficiently using an inverter. In the
next blog, I will explain the application of the method in binary systems and
see how to build a circuit to add or subtract.&lt;/p&gt;</content><author><name>Matthew Tang</name></author><category term="Arithmetic" /><summary type="html">The first series of my blogs is to explain how we build a digital circuit for addition and subtraction – the most fundamental arithmetic operations. I will start with some theory of the maths and then discuss the circuitry. The discussion is a background for us to understand the signed multiplication that I wrote earlier on.</summary></entry><entry><title type="html">Signed Multiplication – Part 1</title><link href="/arithmetic/2020/12/13/signed-multiplication-1.html" rel="alternate" type="text/html" title="Signed Multiplication – Part 1" /><published>2020-12-13T10:02:07+00:00</published><updated>2020-12-13T10:02:07+00:00</updated><id>/arithmetic/2020/12/13/signed-multiplication-1</id><content type="html" xml:base="/arithmetic/2020/12/13/signed-multiplication-1.html">&lt;p&gt;Through the years my students repeatedly confuse with the theory of the signed
multiplication. They do not understand why a correction is needed when the
mutliplier (i.e. \(B\) in \(A\times B\)) is negative. This is of course complicated
by the fact that negative numbers are expressed or represented by its 2’s
complement so that only addition is required through the procedure.&lt;/p&gt;

&lt;p&gt;In this short article, I am going to explain and derive the correction in
signed multiplication and then discuss the implications when the theory is
applied to multiplier circuit design.&lt;/p&gt;

&lt;h1 id=&quot;the-mathematics&quot;&gt;The Mathematics&lt;/h1&gt;

&lt;p&gt;Let’s consider two integers \(A\) and \(B\). We represent them in
binary using exactly \(N\) bits, as bit strings 
\(a_{N-1}a_{N-2}\ldots a_0\) 
and
\(b_{N-1}b_{N-2}\ldots b_0\)
respectively. 
Then these representations give different numerical values depending
whether it is positive or not:&lt;/p&gt;

\[b_{N-1}b_{N-2}\ldots b_0= \left\{
\begin{matrix}
B &amp;amp; \mathrm{when}\ B \geq 0\\
2^N - |B| = 2^N + B &amp;amp;\mathrm{when}\ B &amp;lt; 0\\
\end{matrix}
\right.\]

&lt;p&gt;To avoid subtraction in binary, we convert subtraction into a 2’s complement
operation (equivalent to take a negative of a number) followed by a normal
addition.&lt;/p&gt;

\[A - B = A + (-B)\rightarrow A + (2^N - B)\]

&lt;p&gt;The carry out \(2^N\) should be &lt;strong&gt;ignored&lt;/strong&gt; to keep the answer numerically correct.&lt;/p&gt;

&lt;p&gt;Now consider the binary multiplication \(A\times B\) using the longhand method,
where each bit \(b_i\) is multiplied (or logic AND) to \(a_{N-1}\ldots a_0\) and
then shift \(i\) bits to the left to give a signed subproduct. The subproducts
\(P_i\) are added together to produce the final product \(P\). In order to preserve
the numerical value of \(A\), the additions of subproducts require \(N + 1\) bit in
minimum and every time the partial sum should be signed extended. In other
words, the binary addition, under such conditions, is signed and safe with the
2’s complement representation. We can therefore express \(P\) as:&lt;/p&gt;

\[P = \sum_{i=0}^{N-1} A\times 2^ib_i = A\times\sum_{i=0}^{N-1}2^ib_i\]

&lt;p&gt;When \(B\) is positive, it is obvious that
\(P = A\times B\)
which is the product that we expect, in a total of \(2N\) bits and in 2’s complement
representation.&lt;/p&gt;

&lt;p&gt;Nevertheless, when \(B\) is negative, we can deduce that&lt;/p&gt;

\[P = A\times (2^N - |B|) = A\times (2^N + B) = 2^NA + AB\]

&lt;p&gt;So we need to correct the answer after the binary multiplication by subtracting
\(2^NA\) from \(P\):&lt;/p&gt;

\[\begin{align}
P - 2^NA &amp;amp;= AB\\
AB &amp;amp;= P + 2^N(2^N - A) = P + C
\end{align}\]

&lt;p&gt;Subtraction is once again avoided by taking 2’s complement of \(A\) in \(N\) bits and
shifting that \(N\) bits to the left. The correction is in \(2N\) bits and matches
the bit width of the product \(P\). We let \(C = 2^N(2^N - A)\) the correction value
that should be added to the product when \(B\) is negative.&lt;/p&gt;

&lt;p&gt;To conclude, the &lt;strong&gt;first method&lt;/strong&gt; to correct the answer from the binary
multiplication is:&lt;/p&gt;

\[AB = \left\{
\begin{matrix}
P &amp;amp; \mathrm{when}\ B \geq 0\\
P + C &amp;amp;\mathrm{when}\ B &amp;lt; 0\\
\end{matrix}
\right.\]

&lt;p&gt;If there is a carry at bit position \(2N\) (i.e. \(2^{2N}\)), it should be ignored.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example 1: \(N = 4, A = -6, B = -3\), then&lt;/p&gt;

  &lt;p&gt;\(a = 10000_2 - 0110_2 = 1010_2 = 10\), \(b = 10000_2 - 0011_2 = 1101_2 = 13\)&lt;/p&gt;

  &lt;p&gt;\(P = 10110010_2\) (carry discarded) which represents \(-78 = -6\times 2^4 + (-6)(-3)\)
\(C = 2^4(2^4 - (-6)) = 16(16 + 6) = 352 = 256 + 92\)
\(P + C = -78 + 352 = 256 + 18 = 2^8 + 18\)&lt;/p&gt;

  &lt;p&gt;So once the carry at bit 8 is discarded, the correct product is found.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Correcting the product after the binary multiplication requires a total of \(N +
2\) additions (\(N + 1\) for subproducts and a last one correction). 
This can be reduced back to \(N + 1\) if we merge the correction with the last
addition of subproduct, noting that:&lt;/p&gt;

\[P = \sum_{i=0}^{N-1} A\times 2^ib_i = \sum_{i=0}^{N-2}2^ib_i + 2^{N-1}A\]

&lt;p&gt;when \(B\) is negative and \(b_{N-1} = 1\). Then we have:&lt;/p&gt;

\[\begin{aligned}
AB  &amp;amp;= P + 2^N(2^N - A) \\
    &amp;amp;= \sum_{i=0}^{N-2}2^ib_i + 2^{N-1}A + 2^N(2^N - A)\\
    &amp;amp;= \sum_{i=0}^{N-2}2^ib_i + 2^{N-1}(A + 2(2^N - A))\\
    &amp;amp;= \sum_{i=0}^{N-2}2^ib_i + 2^{N-1}(2^{N+1} - A)\\
    &amp;amp;= P' + C'
\end{aligned}\]

&lt;p&gt;\(C'\) is the 2’s complement of \(A\) in \((N+1)\) bits then shifted \((N-1)\) bits to
the left. \(C'\) is also \(2N\) bits in width and matches that of the partial
products \(P'\).&lt;/p&gt;

&lt;p&gt;This gives the &lt;strong&gt;second method&lt;/strong&gt; to correct the answer from the binary multiplication:&lt;/p&gt;

\[AB = \left\{
\begin{matrix}
P &amp;amp; \mathrm{when}\ B \geq 0\\
P' + C' &amp;amp;\mathrm{when}\ B &amp;lt; 0\\
\end{matrix}
\right.\]

&lt;p&gt;Similarly, if there is a carry at bit position \(2N\) (i.e. \(2^{2N}\)), it should be ignored.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example 2: \(N = 4, A = -6, B = -3\)&lt;/p&gt;

  &lt;p&gt;\(a = 10000_2 - 0110_2 = 1010_2 = 10\), \(b = 10000_2 - 0011_2 = 1101_2 = 13\)&lt;/p&gt;

  &lt;p&gt;\(P' = 11100010_2\), \(C' = 2^3(2^5 - (-6)) = 00110000_2\) 
(if you convert from 560 then you need to drop the leading &lt;em&gt;10&lt;/em&gt; bits and leave the least 8 bits)
\(P' + C' = 11100010_2 + 00110000_2 = \cancel{1}00010010_2\) 
which gives &lt;strong&gt;18&lt;/strong&gt; when carry out is discarded.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the next part, I will discuss what this mathematical analysis means to the multiplier design in digital electronics.&lt;/p&gt;</content><author><name>Matthew Tang</name></author><category term="Arithmetic" /><summary type="html">Through the years my students repeatedly confuse with the theory of the signed multiplication. They do not understand why a correction is needed when the mutliplier (i.e. \(B\) in \(A\times B\)) is negative. This is of course complicated by the fact that negative numbers are expressed or represented by its 2’s complement so that only addition is required through the procedure.</summary></entry><entry><title type="html">My new blog</title><link href="/general/2020/12/12/my-new-blog.html" rel="alternate" type="text/html" title="My new blog" /><published>2020-12-12T08:00:00+00:00</published><updated>2020-12-12T08:00:00+00:00</updated><id>/general/2020/12/12/my-new-blog</id><content type="html" xml:base="/general/2020/12/12/my-new-blog.html">&lt;p&gt;This is the first post in this new blog of mine. After teaching engineering of
digital systems large and small for a decade, I feel that there are much to
share with the others. While we are still in the pandemic, blogging seems to be
the best way to share and communicate with other each.&lt;/p&gt;

&lt;p&gt;In here I would like to keep things simple and easy to understand for most
readers without a technical background, in the same way I would do to my
students.
I decided to start with many topics that confuses my students in the digital
system design classes. For example, the theory behind computer arithmetic and
the design of arithmetic circuits in digital technolgies. Later I will also
write about interfacing between software (i.e. our programs) and hardware (i.e.
our computer).&lt;/p&gt;

&lt;p&gt;Of course I would also love to write short stories of my professional work,
whenever it is related digital electronics and systems.&lt;/p&gt;

&lt;p&gt;If you have any comments about my articles, feel free to get in touch through my
contacts from &lt;a href=&quot;/about/&quot;&gt;the about page&lt;/a&gt;.&lt;/p&gt;</content><author><name>Matthew Tang</name></author><category term="General" /><summary type="html">This is the first post in this new blog of mine. After teaching engineering of digital systems large and small for a decade, I feel that there are much to share with the others. While we are still in the pandemic, blogging seems to be the best way to share and communicate with other each.</summary></entry></feed>