Category:I600 Introduction to Computers and Informatics: Difference between revisions

From ICO wiki
Jump to navigationJump to search
Lvosandi (talk | contribs)
Lvosandi (talk | contribs)
 
(225 intermediate revisions by 18 users not shown)
Line 2: Line 2:
The course will follow roughly the same structure as [https://lagunita.stanford.edu/courses/Engineering/CS101/Summer2014/about Computer Science 101 at Stanford University] with more hands-on approach. This course substitutes Estonian courses I100 Sissejuhatus informaatikasse and [http://www.pld.ttu.ee/~teet/itarvutid.html I201 Arvutid].
The course will follow roughly the same structure as [https://lagunita.stanford.edu/courses/Engineering/CS101/Summer2014/about Computer Science 101 at Stanford University] with more hands-on approach. This course substitutes Estonian courses I100 Sissejuhatus informaatikasse and [http://www.pld.ttu.ee/~teet/itarvutid.html I201 Arvutid].


Monday lectures will cover more theoretical stuff;
Feedback form: https://goo.gl/forms/evqt69NeRkf5IPKx1
Thursday workshops are for getting started with new topics;
the homework assignments connect theoretical with practice and
Tuesday sessions are for follow up, asking/answering questions and presenting homework.
Attendance won't be tracked, the sessions are there if you need help.
Bring your laptops for Tuesday and Thursday sessions.


Video recordings: [https://echo360.e-ope.ee/ess/portal/section/f7f2d8d7-7395-40c7-841f-b4522e55e422 2016 fall], [https://echo360.e-ope.ee/ess/portal/section/b685b998-4385-4632-8cb7-5665525fbdde 2015 fall].


Course credits: 6 ECTS
Course credits: 6 ECTS
Line 35: Line 31:


Exam will be halfway between oral and written: You'll be given 30 minutes to prepare for several random questions and 15 minutes to discuss what you've answered.
Exam will be halfway between oral and written: You'll be given 30 minutes to prepare for several random questions and 15 minutes to discuss what you've answered.
If you don't have prior experience with the topics, it's highly recommended to take part of the sessions in order to pass the exam.
If you don't have prior experience with the topics, it's highly recommended to take part of the sessions in order to pass the exam. You can use a calculator and this wiki page, don't rely on the access to terminal or other software tools. You can not access Internet or any remote computers.
 
Exam sections:
 
* Execution of ~10 processor instructions
* Bunch of randomly selected questions from this page
* TBD


=Lecture: Computer hardware=
=Lecture: Computer hardware=


Jargon: CPU, RAM, ROM, HDD, SSD, PCI, PCI Express, USB 2.0, USB 3.0, VGA, HDMI, DVI, LCD, TFT, LED, OLED, AMOLED, CRT, PWM
Jargon: CPU, RAM, ROM, HDD, SSD, PCI, PCI Express, USB 2.0, USB 3.0, VGA, HDMI, DVI, LCD, TFT, LED, OLED, AMOLED, CRT, PWM
[https://echo360.e-ope.ee/ess/echo/presentation/beef71c3-044c-43c9-a326-e1e62cbb5c9b Lecture recording #1]
[https://echo360.e-ope.ee/ess/echo/presentation/36786bbb-84b6-4604-be6f-c363bf22ce8d Lecture recording #2 starting 12:30]


[https://docs.google.com/presentation/d/1OzaqTBAUfMOzhG0HZeSJVSszCmFyn4ug35teYJWkyfM/edit?usp=sharing Lecture slides] Random access memory, permanent storage, buses, input devices, display technologies, networking
[https://docs.google.com/presentation/d/1OzaqTBAUfMOzhG0HZeSJVSszCmFyn4ug35teYJWkyfM/edit?usp=sharing Lecture slides] Random access memory, permanent storage, buses, input devices, display technologies, networking
Line 67: Line 65:


Security section: [http://www.bunniestudios.com/blog/?p=3554 Hacking SD cards]
Security section: [http://www.bunniestudios.com/blog/?p=3554 Hacking SD cards]
Homework:
* Watch [https://www.youtube.com/watch?v=z47Gv2cdFtA Fairchild Semiconductor educational video] about integrated cicruits
* Check if your computer is susceptible to [https://github.com/google/rowhammer-test Rowhammer] attack


Assignments:
Assignments:
* [https://wiki.itcollege.ee/index.php/I600_Introduction_to_Computers_and_Informatics#Assignment:_Investigating_PC_hardware Investigating PC hardware]
* [https://wiki.itcollege.ee/index.php/I600_Introduction_to_Computers_and_Informatics#Assignment:_Investigating_PC_hardware Investigating PC hardware]
* [https://wiki.itcollege.ee/index.php/I600_Introduction_to_Computers_and_Informatics#Assignment:_Investigating_embedded_hardware Investigating embedded hardware]
* [https://wiki.itcollege.ee/index.php/I600_Introduction_to_Computers_and_Informatics#Assignment:_Investigating_embedded_hardware Investigating embedded hardware]
#test


=Lecture: Storage abstractions=
=Lecture: Storage abstractions=
Line 77: Line 81:


[https://docs.google.com/presentation/d/1dJ-HMSR9nW8OuZtSTYf5dlFyPsZKGqxlKoEwIHi9h6k/ Lecture slides]  
[https://docs.google.com/presentation/d/1dJ-HMSR9nW8OuZtSTYf5dlFyPsZKGqxlKoEwIHi9h6k/ Lecture slides]  
[https://echo360.e-ope.ee/ess/echo/presentation/36786bbb-84b6-4604-be6f-c363bf22ce8d Lecture recording] starts at 47:50.


* What is a [https://en.wikipedia.org/wiki/Block_%28data_storage%29 block device]?
* What is a [https://en.wikipedia.org/wiki/Block_%28data_storage%29 block device]?
Line 91: Line 93:




[https://echo360.e-ope.ee/ess/echo/presentation/04eeebd1-c892-485a-9123-03abaa3ce3ca Lecture recording #1], first half we'll discuss about first assignment about investigating PC hardware, slides start at 43:40.
[https://echo360.e-ope.ee/ess/echo/presentation/26f1d40d-7830-4e5c-b783-e061c1e91cfc Lecture recording #2], first 15 minutes we'll discuss about second assignment about investigating embedded hardware, last slides about kernels are discussed from 16:00 up to 27:30. I forgot to switch video input for lecture recording so you'll have to browse slides from the link below :/


Lecture slides:
Lecture slides:
Line 114: Line 113:


=Lecture: Libraries, frameworks=
=Lecture: Libraries, frameworks=
[https://echo360.e-ope.ee/ess/echo/presentation/26f1d40d-7830-4e5c-b783-e061c1e91cfc Lecture recording #1], from 00:27:30 up to 1:04:00.


[https://docs.google.com/presentation/d/1q9kGg5AI3AyXglxpJdLjLMpgRLdbF9dl62gauNmDEG8/edit Lecture slides]
[https://docs.google.com/presentation/d/1q9kGg5AI3AyXglxpJdLjLMpgRLdbF9dl62gauNmDEG8/edit Lecture slides]
Line 126: Line 123:


In this lecture we'll talk about programming languages
In this lecture we'll talk about programming languages
Lecture recording: [https://echo360.e-ope.ee/ess/echo/presentation/26f1d40d-7830-4e5c-b783-e061c1e91cfc starting from 1:04:00]


Lecture slides: [https://docs.google.com/presentation/d/1CCZntY7Z-AG_Mice5N2kclDGYI2xVVvRHgWUIs-SxYo/edit programming languages, stack machine]
Lecture slides: [https://docs.google.com/presentation/d/1CCZntY7Z-AG_Mice5N2kclDGYI2xVVvRHgWUIs-SxYo/edit programming languages, stack machine]
Line 141: Line 136:
Assignments:
Assignments:


* [https://wiki.itcollege.ee/index.php/Category:I600_Introduction_to_Computers_and_Informatics#Assignment:_Disassembling_Python Disassembling Python]
* [https://wiki.itcollege.ee/index.php/Category:I600_Introduction_to_Computers_and_Informatics#Assignment:_Investigating_compilers Investigating compilers]
* [https://wiki.itcollege.ee/index.php/Category:I600_Introduction_to_Computers_and_Informatics#Assignment:_Investigating_compilers Investigating compilers]


=Lecture: Data encoding=
=Lecture: Data encoding=
[https://echo360.e-ope.ee/ess/echo/presentation/ca8310e9-e82e-4e47-b90a-2e1801bcfada Lecture recording]


[https://docs.google.com/presentation/d/1QV_IMOadIEUTBW7Laqw59j8J7Y8HKwvSDmP-swEEx0s Lecture slides]
[https://docs.google.com/presentation/d/1QV_IMOadIEUTBW7Laqw59j8J7Y8HKwvSDmP-swEEx0s Lecture slides]
Line 155: Line 147:


* What is bit? Nibble? Byte? Word?
* What is bit? Nibble? Byte? Word?
* Write 9375 in binary, hexadecimal?
* Write 0xDEADBEEF in decimal?
* How are dates/time stored in binary?
* What is [https://en.wikipedia.org/wiki/Quantization_%28signal_processing%29 quantization] in terms of signal processing?
* What is [https://en.wikipedia.org/wiki/Quantization_%28signal_processing%29 quantization] in terms of signal processing?
* How are [https://en.wikipedia.org/wiki/Integer_%28computer_science%29 integers] stored in binary? What integer range can be described using n bits? How many bits are required to describe integer range from n .. m.
* How are [https://en.wikipedia.org/wiki/Integer_%28computer_science%29 integers] stored in binary? What integer range can be described using n bits? How many bits are required to describe integer range from n .. m.
Line 171: Line 166:
Relevant assignments:
Relevant assignments:


* [https://wiki.itcollege.ee/index.php/Category:I600_Introduction_to_Computers_and_Informatics#Assignment:_Disassembling_Python Disassembling Python]
* [https://wiki.itcollege.ee/index.php/Category:I600_Introduction_to_Computers_and_Informatics#Assignment:_Investigating_compilers Investigating compilers]
* [https://wiki.itcollege.ee/index.php/Category:I600_Introduction_to_Computers_and_Informatics#Assignment:_Investigating_compilers Investigating compilers]


=Lecture: Introduction to code execution=
=Lecture: Code execution in processor=


[https://docs.google.com/presentation/d/18wPDLydyZevMi9F0MX7F6eK-NMBZQzGN4mviF_ft8-E/edit Lecture slides]
[https://docs.google.com/presentation/d/18wPDLydyZevMi9F0MX7F6eK-NMBZQzGN4mviF_ft8-E/edit Lecture slides]
Line 184: Line 178:
Assignments:
Assignments:


* [https://wiki.itcollege.ee/index.php/Category:I600_Introduction_to_Computers_and_Informatics#Assignment:_Disassembling_Python Disassembling Python]
* [https://wiki.itcollege.ee/index.php/Category:I600_Introduction_to_Computers_and_Informatics#Assignment:_Investigating_compilers Investigating compilers]
* [https://wiki.itcollege.ee/index.php/Category:I600_Introduction_to_Computers_and_Informatics#Assignment:_Investigating_compilers Investigating compilers]


=Lecture: Introduction to ...=
Exam question:
 
* Given ~10 instructions and their explainations, follow the instructions and elaborate after every step what happened in the processor?


Potential exam questions:


* What is [https://en.wikipedia.org/wiki/Moore's_law Moore's law]? What is [https://en.wikipedia.org/wiki/Rock's_law Rock's law]?
=Lecture: Introduction to Boole algebra=
* What are the differences between linear vs switching power regulation? [http://www.digikey.com/en/articles/techzone/2012/may/understanding-the-advantages-and-disadvantages-of-linear-regulators]
* What is [https://en.wikipedia.org/wiki/Electrical_resistance resistance]? [https://en.wikipedia.org/wiki/Capacitance Capacitance]? [https://en.wikipedia.org/wiki/Inductance Inductance]?
* What is [https://en.wikipedia.org/wiki/Resistor resistor]? [https://en.wikipedia.org/wiki/Capacitor Capacitor]? [https://en.wikipedia.org/wiki/Inductor Inductor]? [http://www.explainthatstuff.com/howtransistorswork.html Transistor]?


In this lecture we'll talk about Y
[https://docs.google.com/presentation/d/1Y1rXvt2oXROhW-LZ07B0Sg90D-_u8SlLUZeaxds7f1g/ Lecture slides]


=Lecture: Introduction to Boole algebra=
Exam questions:


* Simplify A AND A OR B
* Show addition of X and Y in binary
* Show subtraction of X and Y  in binary
* Show multiplication of X and Y in binary




Line 205: Line 200:
* [https://wiki.itcollege.ee/index.php/Category:I600_Introduction_to_Computers_and_Informatics#Assignment:_Designing_arithmetic-logic_unit Designing arithmetic-logic unit]
* [https://wiki.itcollege.ee/index.php/Category:I600_Introduction_to_Computers_and_Informatics#Assignment:_Designing_arithmetic-logic_unit Designing arithmetic-logic unit]


=Lecture: Hardware description languages=
[https://docs.google.com/presentation/d/1HO9iPfi8S3ZykuW7IHVm09QamLHiVo6s6lUGwWu4eD4/ Lecture slides]
Potential exam questions:
* What are the uses for hardware description languages?
* What is latch?
* What is flip-flop?
* What is mux (multiplexer)?
* What is register? Register file?
* What is ALU?
* What is floating-point unit?
* What is a cache?
* What is a bus?
* Show the circuit diagram for A OR B AND C, NOT A AND B, <insert some other Boole formula here>?
* Show the truth table for <insert Boole formula here>?
* Write the equivalent Boole formula of a circuit diagram.
=Lecture: Publishing work=
[https://docs.google.com/presentation/d/1hCqRjGUx5Yv6iZN4xaz2UICLWbycnM46ctw2zX0g94U/ Lecture slides]
Potential exam questions:
* What are the major implications of MIT, BSD and GPL licenses?
* What are the differences between copyright, trademark, trade secret?
* Where would you use waterfall software development model? Where would you use agile?
* What is the purpose of a version control system?
* What would you store in a version control system?
=Lecture: Algorithms and data structures=
[https://docs.google.com/presentation/d/1vwJvvRerM0rYBaiE3pEX5GUXRHuVJvjTUDKo8fqTq_A/ Lecture slides]
[https://docs.google.com/presentation/d/1xK-npNPn7zp0ysn_N8yBg3dF3jQT0jNPEnZRAe0pmqk/edit?usp=sharing Lecture slides (shortened)]
Potential exam questions:
* What is time complexity of algorithm?
* What is space complexity of algorithm?
* What's a good algorithm?
=Lecture: History=
Topics: Computer history, Silicon Valley, standards
[https://docs.google.com/presentation/d/1Ybld2ABpm9QJSLL-Bodr1a5XI5Hk_KM2q_nZVYu2L0c/edit?usp=sharing Lecture slides]
Potential example questions:


* What is [https://en.wikipedia.org/wiki/Moore's_law Moore's law]? What is [https://en.wikipedia.org/wiki/Rock's_law Rock's law]?
* What were the major contributing factors for success of Microsoft, Apple, Google, <your favourite company>?
* What were the major contributing factors to the success of Silicon Valley?


=Assignment: Investigating PC hardware=
=Lecture: Microcontrollers=
 
[https://docs.google.com/presentation/d/1qPyzBBfB1TseEzXjcp4-ZjqSnAwbjDmEqyIYcUGFLG0/edit Lecture slides]
 
Jargon: microprocessor, microcontroller, coprocessor, floating-point unit (FPU), digital signal processor (DSP), field programmable grid array (FPGA), sensors, actuators, digital input/output, analog input, general purpose input/output (GPIO), interrupt, interrupt handler, timer/counter, pulse-width modulation (PWM),
 
Potential exam question:
 
* What distinguishes microcontroller from microprocessor?
* What are the differences between Hardvard architecture and von Neumann architecture?
* What is an interrupt?
* What is an timer?
 
 
=Assignment: Investigating PC hardware (5p)=


Goal: Get familiar with your [https://en.wikipedia.org/wiki/X86 laptop hardware]. Get out of the comfort zone of your primary operating system and try out other operating systems. Learn about [https://en.wikipedia.org/wiki/Full_virtualization hardware virtualization].
Goal: Get familiar with your [https://en.wikipedia.org/wiki/X86 laptop hardware]. Get out of the comfort zone of your primary operating system and try out other operating systems. Learn about [https://en.wikipedia.org/wiki/Full_virtualization hardware virtualization].


Deadline: 16. September
Deadline: 26. September
 
Credits: 5 points plus extra point for being extra thorough about interpreting what you see.


Use what you learned in [[Getting started with Ubuntu]] workshop:
Use what you learned in [[Getting started with Ubuntu]] workshop:
Line 220: Line 281:
* Place your preferred ISO image to a memory stick using <code>dd</code> or Win32 Disk Imager and boot it on your personal laptop. You do '''not''' need to install Ubuntu on your harddisk, simply click '''Try Ubuntu''' once the operating system boots off the memory stick and carry out following tasks.
* Place your preferred ISO image to a memory stick using <code>dd</code> or Win32 Disk Imager and boot it on your personal laptop. You do '''not''' need to install Ubuntu on your harddisk, simply click '''Try Ubuntu''' once the operating system boots off the memory stick and carry out following tasks.
* Open up terminal by pressing <code>Ctrl-Alt-T</code>.
* Open up terminal by pressing <code>Ctrl-Alt-T</code>.
* Use <code>lsb_release -a</code> to check what operating system distribution you're running.
* Use <code>lsb_release -a</code> to check what operating system distribution you're running, try <code>apt install lsb-release</code> if command is not available.
* Use <code>uname -sr</code> to check what operating system kernel you're running.  
* Use <code>uname -sr</code> to check what operating system kernel you're running.  
* Use <code>cat /proc/cpuinfo</code> and check processor information. What CPU model and how many cores your computer has?
* Use <code>cat /proc/cpuinfo</code> and check processor information. What CPU model and how many cores your computer has?
* Use <code>arch</code> to check what CPU architecture is being used by the operating system. Is it 32-bit or 64-bit?
* Use <code>arch</code> to check what CPU architecture is being used by the operating system. Is it 32-bit or 64-bit?
* Use <code>cat /proc/meminfo</code> to check memory usage. How much RAM your computer has?
* Use <code>cat /proc/meminfo</code> to check memory usage. How much RAM your computer has?
* Use <code>lspci -t -v -nn</code> to enumerate PCI and PCI Express devices, attempt to identify what is what.
* Use <code>lspci -t -v -nn</code> to enumerate PCI and PCI Express devices, attempt to identify what is what, try <code>apt install pciutils</code> if command is not available.
* Use <code>lsusb</code> and <code>lsusb -t</code> to enumerate USB devices, again attempt to identify what is what.
* Use <code>lsusb</code> and <code>lsusb -t</code> to enumerate USB devices, again attempt to identify what is what, try <code>apt install usbutils</code> if command is not available.
* Use <code>fdisk -l</code> to enumerate disks and partitions. How big is your harddisk? How many and how big partitions it has?
* Use <code>sudo fdisk -l</code> to enumerate disks and partitions. How big is your harddisk? How many and how big partitions it has?
* Use <code>lsblk</code> to enumerate block devices.
* Use <code>lsblk</code> to enumerate block devices.
* Use <code>xrandr</code> to enumerate display outputs. What video output resolutions are available and which one is currently used?
* Use <code>xrandr</code> to enumerate display outputs. What video output resolutions are available and which one is currently used?
Line 248: Line 309:
Note that I can't expect you to install Ubuntu on your physical machine, but I can help if you want to do that. You should have Ubuntu ready to go in a virtual machine at least for subsequent sessions. We're using Ubuntu because it's widely used on servers and in the cloud, so any Ubuntu skills will definitely come handy in future. If you're already familiar with Linux, it's suggested to take a look at other interesting operating systems such as [https://www.freebsd.org/ FreeBSD] or [http://www.openbsd.org/ OpenBSD]. Take a look at [https://www.kali.org/ Kali Linux] if you're interested in penetration testing.
Note that I can't expect you to install Ubuntu on your physical machine, but I can help if you want to do that. You should have Ubuntu ready to go in a virtual machine at least for subsequent sessions. We're using Ubuntu because it's widely used on servers and in the cloud, so any Ubuntu skills will definitely come handy in future. If you're already familiar with Linux, it's suggested to take a look at other interesting operating systems such as [https://www.freebsd.org/ FreeBSD] or [http://www.openbsd.org/ OpenBSD]. Take a look at [https://www.kali.org/ Kali Linux] if you're interested in penetration testing.


=Assignment: Investigating embedded hardware=
=Assignment: Investigating embedded hardware (5p)=


Background: Most of the smartphones nowadays are using [https://en.wikipedia.org/wiki/System_on_a_chip SoC] built around [https://en.wikipedia.org/wiki/ARM_architecture ARM] processor. Raspberry Pi is an excellent piece of hardware to for checking out how an ARM-based machine looks like.
Background: Most of the smartphones nowadays are using [https://en.wikipedia.org/wiki/System_on_a_chip SoC] built around [https://en.wikipedia.org/wiki/ARM_architecture ARM] processor. Raspberry Pi is an excellent piece of hardware to for checking out how an ARM-based machine looks like.
Deadline: 3. October


Goal: Get familiar with hardware ARM-based hardware.
Goal: Get familiar with hardware ARM-based hardware.
Deadline: 23. September
Points: 4 points


Use what you learned in [[Getting started with Ubuntu]] and [[Getting started with Raspberry Pi]] workshops:
Use what you learned in [[Getting started with Ubuntu]] and [[Getting started with Raspberry Pi]] workshops:
Line 267: Line 326:
* Answer to questions above and send it as e-mail to Lauri, make sure you '''use the address supplied above with the course code''', otherwise your mail is not searchable in my mailbox. Attach collected command outputs as [https://en.wikipedia.org/wiki/Plain_text plain text] file, do '''not''' send .odt, .doc files, these are not readable on a phone. When answering to questions phrase the text in a way that is understandable out of context, so it is not necessary to open up wiki to understand what you're talking about.
* Answer to questions above and send it as e-mail to Lauri, make sure you '''use the address supplied above with the course code''', otherwise your mail is not searchable in my mailbox. Attach collected command outputs as [https://en.wikipedia.org/wiki/Plain_text plain text] file, do '''not''' send .odt, .doc files, these are not readable on a phone. When answering to questions phrase the text in a way that is understandable out of context, so it is not necessary to open up wiki to understand what you're talking about.


=Assignment: Investigating LAMP=
=Assignment: Investigating LAMP (5p)=


[https://en.wikipedia.org/wiki/LAMP_%28software_bundle%29 LAMP] software bundle is consists of Linux-based OS, [https://en.wikipedia.org/wiki/Apache_HTTP_Server Apache web server], [https://en.wikipedia.org/wiki/PHP PHP programming language] runtime and [https://en.wikipedia.org/wiki/MySQL MySQL database].
[https://en.wikipedia.org/wiki/LAMP_%28software_bundle%29 LAMP] software bundle is consists of Linux-based OS, [https://en.wikipedia.org/wiki/Apache_HTTP_Server Apache web server], [https://en.wikipedia.org/wiki/PHP PHP programming language] runtime and [https://en.wikipedia.org/wiki/MySQL MySQL database].
Most of the web applications on the Internet including Facebook are built on top of LAMP-styled software stack.
Most of the web applications on the Internet including Facebook are built on top of LAMP-styled software stack.
Use a Raspberry Pi or Ubuntu virtual machine to set up [https://www.raspberrypi.org/learning/lamp-web-server-with-wordpress/ WordPress], [https://doc.owncloud.org/server/8.1/admin_manual/installation/index.html ownCloud] or any well-known web application that makes use of a database such as MySQL.
Use a Ubuntu server virtual machine to set up [https://www.raspberrypi.org/learning/lamp-web-server-with-wordpress/ WordPress], [https://doc.owncloud.org/server/8.1/admin_manual/installation/index.html ownCloud] or any well-known web application that makes use of a database such as MySQL.
To make your life easier also set up SSH public key authentication.
To make your life easier also set up SSH public key authentication.


Goal: Get familiar with LAMP stack. Get comfortable with (SSH) public key authentication.
Goal: Get familiar with LAMP stack. Get comfortable with (SSH) public key authentication.
Deadline: 30. September
Points: 4 points


Tasks:
Tasks:


* If you're using Ubuntu virtual machine approach, see [[Accessing a virtual machine via SSH connection]]
* Install [http://upload.itcollege.ee/iso/ubuntu-16.04.2-server-amd64.iso Ubuntu 16.04 server] in a virtual machine or use [http://upload.itcollege.ee/ova/lamp.ova prepared virtual appliance]
* Configure second network interface as Host-only adapter, this is already done in case of OVA
* Use SSH to connect to your server over the network. If you're using Windows on your laptop use [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html PuTTY] to gain access to command line and [https://winscp.net/ WinSCP] to copy files, otherwise simply boot Ubuntu in a virtual machine and use <code>ssh username@hostname</code> to invoke commands and <code>sftp://username@hostname</code> in the file browser to access filesystem.
* Use SSH to connect to your server over the network. If you're using Windows on your laptop use [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html PuTTY] to gain access to command line and [https://winscp.net/ WinSCP] to copy files, otherwise simply boot Ubuntu in a virtual machine and use <code>ssh username@hostname</code> to invoke commands and <code>sftp://username@hostname</code> in the file browser to access filesystem.
* Set up any of the web applications referenced above. You may be interested in reading also [http://howtoubuntu.org/how-to-install-lamp-on-ubuntu this] and [https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-on-ubuntu-14-04 this].
* Install webserver, MySQL server and PHP, this is already done in case of OVA. Usually you'd do something like <code>apt install apache2 mysql-server php7.0 phpmyadmin</code>
* Demonstrate that the web application you installed works in next Tuesday session, screenshots/photos with explanation sent to e-mail above also suffice.
* Set up any of the web applications referenced above in <code>/var/www/html</code>. You may be interested in reading also [http://howtoubuntu.org/how-to-install-lamp-on-ubuntu this] and [https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-on-ubuntu-14-04 this].
* Demonstrate that the web application you installed works in next lab, screenshots/photos with explanation sent to e-mail above also suffice.
* Optional: Set up SSH public key authentication to <code>enos.itcollege.ee</code>.
* Optional: Set up SSH public key authentication to <code>enos.itcollege.ee</code>.
* Optional: [https://www.raspberrypi.org/documentation/remote-access/ssh/passwordless.md Set up public key authentication to your Raspberry Pi]. Windows users might want to take a look at [https://winscp.net/eng/docs/ui_puttygen#obtaining_and_starting_puttygen PuTTYgen instructions].
* Optional: [https://www.raspberrypi.org/documentation/remote-access/ssh/passwordless.md Set up public key authentication to your Ubuntu box]. Windows users might want to take a look at [https://winscp.net/eng/docs/ui_puttygen#obtaining_and_starting_puttygen PuTTYgen instructions].


Note: You don't have to necessarily use Raspberry Pi - Web application installed in Ubuntu VM is also accepted, also if you're maintaining similar installation of a production server that is accepted as well. [http://www.wampserver.com/en/ WAMP] on Windows is also accepted.
Note: If you're maintaining similar installation of a production server that is accepted as well. [http://www.wampserver.com/en/ WAMP] on Windows is also accepted.


=Assignment: Set up basic IoT scenario=
=Assignment: Set up basic IoT scenario (5p)=
 
NB! This year ESP32 instead of Raspberry Pi, see instructions [https://lauri.xn--vsandi-pxa.com/2017/06/espressif.html here]


[https://en.wikipedia.org/wiki/Internet_of_Things Internet of Things] is one of the emerging technologies (read: hype). IoT is essentially about getting everything online, including lightbulbs, switches, window shades etc. In this assignment LED symbolizes a light and the task is to implement code which allows user to switch the LED on and off over the network.
[https://en.wikipedia.org/wiki/Internet_of_Things Internet of Things] is one of the emerging technologies (read: hype). IoT is essentially about getting everything online, including lightbulbs, switches, window shades etc. In this assignment LED symbolizes a light and the task is to implement code which allows user to switch the LED on and off over the network.


[[File:Raspberry-pi-gpio-18-led bb.png|600px]]
[[File:Rpi led.png|400px]]


Goal: Get familiar how Python code can be started up. Build basic [https://en.wikipedia.org/wiki/Internet_of_Things IoT] appliance, a light that can be turned on and off via the Internet.
Goal: Get familiar how Python code can be started up. Build basic [https://en.wikipedia.org/wiki/Internet_of_Things IoT] appliance, a light that can be turned on and off via the Internet.
Deadline: 7. October
Points: 4 points


* Complete the [http://codeacademy.com/tracks/python Python track at CodeAcademy] if you haven't done that yet.
* Complete the [http://codeacademy.com/tracks/python Python track at CodeAcademy] if you haven't done that yet.
Line 312: Line 367:
* Optional: [http://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/ Smoothen the transitions using PWM].
* Optional: [http://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/ Smoothen the transitions using PWM].


=Assignment: Collaborating using Git=
=Assignment: Collaborating using Git (5p)=


Software development is usually done by several contributors, to facilitate efficient collaboration a distributed version control system is a must. In this assignment you'll upload your code to GitHub and modify fellow student's code to reflect changes in the Raspberry Pi setup:
Software development is usually done by several contributors, to facilitate efficient collaboration a distributed version control system is a must. In this assignment you'll upload your code to GitHub and modify fellow student's code to reflect changes in the Raspberry Pi setup:


[[File:Raspberry-pi-rgb-led bb.png|600px]]
[[File:Raspberry-pi-rgb-led bb.png|600px]]
Deadline: 14. October
Points: 5


Goal: Get familiar with distributed version control systems. Collaborate.
Goal: Get familiar with distributed version control systems. Collaborate.
Line 337: Line 388:
* Use <code>git clone</code> to clone the repository to your laptop. Ubuntu should first <code>apt-get install git</code>, Windows and Mac users might want to take a look at [http://git-scm.com/download/ Git homepage]. If you're looking for graphical user interface take a look at [https://desktop.github.com/ GitHub Desktop] or [https://tortoisegit.org/ TortoiseGit].
* Use <code>git clone</code> to clone the repository to your laptop. Ubuntu should first <code>apt-get install git</code>, Windows and Mac users might want to take a look at [http://git-scm.com/download/ Git homepage]. If you're looking for graphical user interface take a look at [https://desktop.github.com/ GitHub Desktop] or [https://tortoisegit.org/ TortoiseGit].
* Create [https://en.wikipedia.org/wiki/README README] in the repository directory, populate it with relevant content - what is it about, who made it etc and commit the changes.
* Create [https://en.wikipedia.org/wiki/README README] in the repository directory, populate it with relevant content - what is it about, who made it etc and commit the changes.
* In your Raspberry Pi setup replace the single-color LED with RGB LED as shown above.
* In your Raspberry Pi setup RGB LED should be connected as shown above.
* Clone '''fellow student's''' repository and adapt the code to reflect physical changes to the setup, the Python snippet should now permit changing the color of the light, have it blinking and turn it off. Other interesting modes are awarded with extra points.
* Clone '''fellow student's''' repository and adapt the code to reflect physical changes to the setup, the Python snippet should now permit changing the color of the light, have it blinking and turn it off. Other interesting modes are awarded with extra points, eg use [http://html5tutorial.info/html5-range.php HTML5 input type range].
* Use either [https://www.atlassian.com/git/tutorials/ Atlassian Tutorials], [http://rogerdudler.github.io/git-guide/ git - the simple guide] or [https://try.github.io/ Try Git] as a reference if you get lost.
* Use either [https://www.atlassian.com/git/tutorials/ Atlassian Tutorials], [http://rogerdudler.github.io/git-guide/ git - the simple guide] or [https://try.github.io/ Try Git] as a reference if you get lost.
* Document in the README what GPIO pins are used in the code for red, green and blue. Extra points for making the Python code [https://docs.python.org/2/library/argparse.html#module-argparse configurable from command-line].
* Document in the README what GPIO pins are used in the code for red, green and blue. Extra points for making the Python code [https://docs.python.org/2/library/argparse.html#module-argparse configurable from command-line].
Line 344: Line 395:
* Send Lauri the URL of your repository at GitHub.
* Send Lauri the URL of your repository at GitHub.


=Assignment: Disassembling Python=
Git resources: [http://git-scm.com/book/en/v2 Pro Git Book] (read the basics to get kickstarted) and [https://www.codecademy.com/en/courses/learn-git/ Codecademy Git course].
 
CPython is the default, most widely used implementation of the Python programming language. CPython is written in C. CPython compiles Python source into bytecode which is interpreted by a Python virtual (stack) machine. This is very similar to what is done with Java. Use [https://docs.python.org/2/library/dis.html dis] to disassemble a Python function into stack machine instructions. Follow [https://docs.python.org/2/library/dis.html#python-bytecode-instructions bytecode instructions] to determine what each instruction does.
 
Deadline: 21. October
 
 
Consider following Python function, which calculates the sum of squares. For example sum_of_powers(2, 3) == 14 (1**2 + 2**2 + 3**2):
 
def sum_of_powers(exponent, numbers):
    counter = 1
    return_value = 0
    while True:
        if counter > numbers:
            break
        return_value += counter ** exponent
        counter += 1
    return return_value
# Following two lines are the ones which produce the instructions shown below
from dis import dis
dis(sum_of_powers)


Corresponding Python stack machine instructions extracted with <code>dis</code> are following. Columns correspond to: line number in the source code, instruction offset (in bytes), instruction name as listed [https://docs.python.org/2/library/dis.html in dis documentation],
=Assignment: Investigating compilers (15p)=
 
22          0 LOAD_CONST              1 (1)              # Constant of 1 is pushed to stack
              3 STORE_FAST              2 (counter)        # Pushed value is popped and stored in variable counter
23          6 LOAD_CONST              2 (0)              # Constant of 0 is pushed to stack
              9 STORE_FAST              3 (return_value)    # Pushed value is popped and stored in variable return_value
24          12 SETUP_LOOP              43 (to 58)          # Loop until instruction no 58 is set up
25    >>  15 LOAD_FAST                2 (counter)            # Value of variable <counter> is pushed to stack
            18 LOAD_FAST                1 (numbers)            # Value of variable <numbers> is pushed to stack
            21 COMPARE_OP              4 (>)                  # Two topmost values of stack are popped and compared,
                                                                # boolean result is pushed back to the top of stack
            24 POP_JUMP_IF_FALSE      31                      # If top of the stack if False,
                                                                # jump to instruction no 31 is performed.
                                                                # Top of the stack is popped
26          27 BREAK_LOOP                                      # Execution jumps out of the loop, the one
                                                                # that was set up at inst no 12
            28 JUMP_FORWARD            0 (to 31)              # Placeholder probably due to the way pipeline is handled
27    >>  31 LOAD_FAST                3 (return_value)        # Value of variable <return_value> is pushed to stack
            34 LOAD_FAST                2 (counter)            # Value of variable <counter> is pushed to stack
            37 LOAD_FAST                0 (exponent)            # Value of variable <exponent> is pushed to stack
            40 BINARY_POWER                                    # Exponentiation of two topmost values is performed and
                                                                # substituted with the result
            41 INPLACE_ADD                                      # Two topmost values are added and substituted with the sum
            42 STORE_FAST              3 (return_value)        # Sum of the addition is stored in variable <return_value>
28          45 LOAD_FAST                2 (counter)            # Value of variable <counter> is pushed to stack
            48 LOAD_CONST              1 (1)                  # Constant of 1 is pushed to stack
            51 INPLACE_ADD                                      # Two topmost values are added and substituted with the sum
            52 STORE_FAST              2 (counter)            # Sum is stored in the variable <counter>
            55 JUMP_ABSOLUTE          15                      # Code execution jumps to instruction no 15 (the beginning of the loop)
29    >>  58 LOAD_FAST                3 (return_value)    # The value of variable <return_value> is pushed to stack
            61 RETURN_VALUE                                # Return top of the stack to the function caller
 
 
Executing the function with arguments exponent=2 and numbers=3 will result in return value of 14, the steps to get to the results are following:
 
    -1. stack = []; co_varnames = [2, 3]; co_consts[?, 1, 0]
    0. stack = [1]
    3. stack = []; co_varnames = [2, 3, 1]
    6. stack = [0];
    9. stack = []; co_varnames = [2, 3, 1, 0]
    12. stack = []; loop is set up, counter is 1
   
    15. stack = [1]
    18. stack = [1, 3]
    21. stack = [False]
    24. stack = []; jump to instruction 31
    31. stack = [0];
    34. stack = [0, 1];
    37. stack = [0, 1, 2];
    40. stack = [0, 1]
    41. stack = [1]
    42. stack = []; co_varnames = [2, 3, 1, 1]
    45. stack = [1];
    48. stack = [1, 1];
    51. stack = [2]
    52. stack = []; co_varnames = [2, 3, 2, 1]
    53. jump to instruction no 15, counter is 2
   
    15. stack = [2]
    18. stack = [2, 3]
    21. stack = [False]
    24. stack = []; jump to instruction 31
    31. stack = [1]; here we have only 1**1
    34. stack = [1, 2];
    37. stack = [1, 2, 2];
    40. stack = [1, 4]
    41. stack = [5]; here we already have 1**2 + 2**2
    42. stack = []; co_varnames = [2, 3, 2, 5]
    45. stack = [2];
    48. stack = [2, 1];
    51. stack = [3]
    52. stack = []; co_varnames = [2, 3, 3, 5]
    53. jump to instruction no 15, counter is 3
   
    15. stack = [3]
    18. stack = [3, 3]
    21. stack = [False]
    24. stack = []; jump to instruction 31
    31. stack = [5]
    34. stack = [5, 3];
    37. stack = [5, 3, 2];
    40. stack = [5, 9]
    41. stack = [14]; here we have 1**2 + 2**2 + 3**2
    42. stack = []; co_varnames = [2, 3, 3, 14]
    45. stack = [3];
    48. stack = [3, 1];
    51. stack = [4]
    52. stack = []; co_varnames = [2, 3, 4, 14]
    53. jump to instruction no 15, counter is 4
   
    15. stack = [4]
    18. stack = [4, 3]
    21. stack = [True]
    24. stack = []; no jump
    27. break out of the loop
    58. stack = [14]
    61. return to caller
 
Assignment steps:
 
* Select one of the Fibonacci code examples below
* Use <code>from dis import dis</code> and <code>dis(function_name_goes_here)</code> to disassemble the function to opcodes
* Comment the stack machine instructions as shown above
* Follow the instructions and elaborate after each instruction executed a) the stack contents b) co_varnames contents c) co_names contents d) what happened as shown above
 
 
Fibonacci with iterators:
 
<pre>
def fib(n):
    a,b = 1,1
    for i in range(n-1):
        a,b = b,a+b
    return a
</pre>
 
Fibonacci with recursion:
 
<pre>
def fib(n):
    if n==1 or n==2:
        return 1
    return fib(n-1) + fib(n-2)
</pre>
 
Fibonacci with infinite loop and breaks:
 
<pre>
def fib3(n):
    a,b,i = 1,1,0
    while True:
        i += 1
        if i >= n: break
        b,a = a+b,b
    return a
</pre>
 
Another variation:
 
<pre>
def fib4(n):
    a = 1
    b = 1
    i = 0
    while True:
        i += 1
        if i >= n: break
        s = a + b
        a = b
        b = s
    return a
</pre>
 
=Assignment: Investigating compilers=


Use what you learned in the [https://wiki.itcollege.ee/index.php/Getting_started_with_GCC Getting started with GCC] session.
Use what you learned in the [https://wiki.itcollege.ee/index.php/Getting_started_with_GCC Getting started with GCC] session.
Line 537: Line 407:
<Insert extemely useful link here yourself>
<Insert extemely useful link here yourself>


Deadline: 4. November
==Example==


If you don't have Ubuntu installed on your machine skip next step and use SSH to perform these steps remotely at <code>enos.itcollege.ee</code>.
If you don't have Ubuntu installed on your machine skip next step and use SSH to perform these steps remotely at <code>enos.itcollege.ee</code>.
Otherwise install ARM cross-compiler:
Otherwise install ARM cross-compiler:


  apt-get install gcc-arm-linux-gnueabihf
  apt-get install gcc-arm-linux-gnueabi


Consider C snippet for calculating integer [https://en.wikipedia.org/wiki/Exponentiation exponentiation], place it in a file <code>power.c</code>:
Consider C snippet for calculating integer [https://en.wikipedia.org/wiki/Exponentiation exponentiation], place it in a file <code>power.c</code>:
Line 553: Line 423:
     return result;
     return result;
  }
  }
===Generating assembly===


Compile ARM assembly of the code snippet:
Compile ARM assembly of the code snippet:


  arm-linux-gnueabi-gcc -S power.c -O power.s  # Cross-compile ARM assembly file power.s from C source code file power.c
  arm-linux-gnueabi-gcc -Os -S power.c -o power.s  # Cross-compile ARM assembly file power.s from C source code file power.c
  cat power.s                                   # Dump the assembly file on terminal
  cat power.s                                       # Dump the assembly file on terminal


The compiler should produce something similar to following:
The compiler should produce something similar to following:
Line 596: Line 468:
  .ident "GCC: (Ubuntu/Linaro 4.7.3-12ubuntu1) 4.7.3"
  .ident "GCC: (Ubuntu/Linaro 4.7.3-12ubuntu1) 4.7.3"
  .section .note.GNU-stack,"",%progbits
  .section .note.GNU-stack,"",%progbits
 
===Commenting the assembly===


You can safely ignore the lines starting with dot, these are simply hints for the next stage of the compilation process.
You can safely ignore the lines starting with dot, these are simply hints for the next stage of the compilation process.
Thus you're left only (!!!) 10 actual instructions. Here you can also see what kind of comments are expected from the submission:
Thus you're left only (!!!) 10 actual instructions. Here you can also see what kind of comments are expected from the submission:


/* Function is invoked with arguments (base, exponent) already placed in r0 and r1 */
  power:
  power:
  cmp r1, #0            /* Compare register r1 contents against constant 0 */     
  cmp r1, #0            /* Compare register r1 (second argument) contents against constant 0 */     
  mov r2, #1            /* Copy the constant 1 to register r2 */
  mov r2, #1            /* Copy the constant 1 to register r2 */
  ble .L2                /* Jump to label .L2 if the comparison was false */
  ble .L2                /* Jump to label .L2 if the comparison was false */
Line 609: Line 481:
  .L3:
  .L3:
  add r3, r3, #1        /* Perform r3 = r3 + 1 */
  add r3, r3, #1        /* Perform r3 = r3 + 1 */
  cmp r3, r1            /* Compare register r3 and r1 */
  cmp r3, r1            /* Compare register r3 and r1 (second argument) */
  mul r2, r0, r2        /* Perform r2 = r0 * r2 */
  mul r2, r0, r2        /* Perform r2 = r0 (first argument) * r2 */
  bne .L3                /* If r3 and r1 were different jump to label .L3
  bne .L3                /* If r3 and r1 were different jump to label .L3
  .L2:
  .L2:
  mov r0, r2            /* Copy register r2 contents to register r0 */
  mov r0, r2            /* Copy register r2 contents to register r0 */
  bx lr                /* Jump back to caller */
  bx lr                /* Jump back to caller */
/* Function finishes with return value placed in r0 */


===Analyzing the code===


Code analysis reveals that registers are mapped as following:
Code analysis reveals that registers are mapped as following:
Line 624: Line 497:
* <code>r2</code> - variable <code>result</code>
* <code>r2</code> - variable <code>result</code>
* <code>r3</code> - variable <code>counter</code>
* <code>r3</code> - variable <code>counter</code>
We can also see how the <code>for</code> loop gets translated into assembly:
power:
cmp r1, #0
mov r2, #1
ble .L2
mov r3, #0
.L3:
add r3, r3, #1 <---+
cmp r3, r1        | 4 instructions are executed repeatedly
mul r2, r0, r2    | if exponent is greater than 0
bne .L3 -----------+
.L2:
mov r0, r2
bx lr
===Stepping through the instructions===
When we execute the function with for example arguments base=5 and exponent=3 following happens in the processor.
Note that this will essentially calculates 5 ^ 3 (5 to the power of 3) which is 125.
Following has been done manually to illustrate how much time does it take to execute the the function.
/* When processor enters the function body the arguments are already placed in registers r0=5 (base), r1=3 (exponent) */
cmp r1, #0                        /* Compare exponent to 0 */
mov r2, #1                        /* Place constant 1 in register r2,
                                      this corresponds to result = 1 in C code */
ble .L2                          /* Exponent was not less than 0, so no jump to L2
mov r3, #0                        /* Place constant 0 in register r3,
                                      this corresponds to variable counter */
add r3, r3, #1                    /* Perform r3 = 0 + 1 which results in 1 being stored to r3
                                      this corresponds to first invocation of counter++ in C code */
cmp r3, r1                        /* Compare counter (1 in this case) to exponent (3), this will be used by bne instruction below */
mul r2, r0, r2                    /* Perform r2 = r0 * r2 which results in 1 * 5 = 5 being placed in r2
                                      this corresponds to first invocation of result *= base in C code */
bne .L3                          /* The comparison resulted in counter being not equal to exponent, so we jump back to L3
                                      this corresponds to first invocation of counter < exponent in C code */
add r3, r3, #1                    /* Perform r3 = 1 + 1 which results in 2 being stored to r3
                                      this corresponds to second invocation of counter++ in C code */
cmp r3, r1                        /* Compare counter (2 in this case) to exponent (3), this will be used by bne instruction below */
mul r2, r0, r2                    /* Perform r2 = r0 * r2 which results 5 * 5 = 25 being placed in r2
                                      this corresponds to second invocation of result *= base in C code */
bne .L3                          /* The comparison resulted in counter being not equal to exponent, so we jump back to L3
                                      this corresponds to second invocation of counter < exponent in C code */
   
   
add r3, r3, #1                    /* Perform r3 = 2 + 1 which results in 3 being stored to r3
                                      this corresponds to third invocation of counter++ in C code */
cmp r3, r1                        /* Compare counter (3 in this case) to exponent (3), this will be used by bne instruction below */
mul r2, r0, r2                    /* Perform r2 = r0 * r2 which results 25 * 5 = 125 being placed in r2
                                      this corresponds to third invocation of result *= base in C code */
bne .L3                          /* The comparison resulted in counter being equal to exponent, so we DO NOT jump back to L3 */
mov r0, r2                    /* Copy register r2 contents (125) to register r0 */
bx lr                        /* Jump back to caller */
/* Function returns with 125 placed in r0 this is where caller function should expect the return value */
/* The other registers will still hold whatever values were left there: r1 = 3, r2 = 125, r3 = 3 */
===ARM registers===
If you get lost with the ARM register naming conventions use following as a guide.
ARM7 has 16x 32-bit registers:
ARM7 has 16x 32-bit registers:


   r0 (a1) - Argument/result/scratch register 4
   r0 (a1) - Argument/result/scratch register, this is where first function argument is usually placed
   r1 (a2) - Argument/result/scratch register 4
   r1 (a2) - Argument/result/scratch register, this is where second function argument is placed
   r2 (a3) - Argument/scratch register 4
   r2 (a3) - Argument/scratch register 3
   r3 (a4) - Argument/scratch register 4  
   r3 (a4) - Argument/scratch register 4
   r4 (v1) - Variable register 1
   r4 (v1) - Variable register 1
   r5 (v2) - Variable register 2
   r5 (v2) - Variable register 2
Line 644: Line 581:
                           executing function should return to?)
                           executing function should return to?)
  r15 (pc) - Program counter (what is the memory address of instruction
  r15 (pc) - Program counter (what is the memory address of instruction
                            that we are going to execute next?)
      ^                      that we are going to execute next?)
      |
      +- These are simply aliases for the numbered registers and
        can be used interchangeably.


Assignment steps:
==Another example==


* Select one of the Fibonacci calculation examples below.
For learning purposes try to do the steps described above for the different flavours of Fibonacci calculation function below:
* Use ARM cross compiler as shown above to generate the assembly corresponding to the C source code.
* Remove the compiler hints (lines starting with dot).
* Analyze and comment the assembly as shown above.
* Use 3 as argument and follow the instructions, what values are left in the registers after the function finishes? Optional/extra point: elaborate step by step what happens in the processor: what instruction is being executed and what values are left in registers after instruction finishes.
* Recompile with different optimization levels: <code>-Os</code>, <code>-O0</code>, <code>-O1</code>, <code>-O2</code> and <code>-O3</code>. What differences did you notice?
* Take a guess which one of the Fibonacci calculation functions below is the slowest? Which one is the fastest?
* Send your conclusions as e-mail attachment to Lauri, attach the commented assembly, make sure you '''use the address supplied above with the course code''', otherwise your mail is not searchable in my mailbox.




Fibonacci calculation using <code>for</code>-loop:
Fibonacci calculation using <code>for</code>-loop:


  int fib(n) {
  int fib(int n) {
     int a = 1;
     int a = 1;
     int b = 1;
     int b = 1;
Line 674: Line 607:
Fibonacci calculation using recursion:
Fibonacci calculation using recursion:


  int fib2(n) {
  int fib2(int n) {
     if (n == 1 || n== 2) {
     if (n == 1 || n== 2) {
         return 1;
         return 1;
Line 684: Line 617:
Fibonacci calculation using <code>while</code>-loop:
Fibonacci calculation using <code>while</code>-loop:


  int fib4(n) {
  int fib4(int n) {
     int a = 1;
     int a = 1;
     int b = 1;
     int b = 1;
Line 698: Line 631:
  }
  }


=Assignment: Investigating microcontrollers=


Deadline: 11. November
==Working with VisUAL==
 
Software itself you will find at [http://salmanarif.bitbucket.org/visual/ VisUAL - A highly visual ARM emulator]
 
Be aware, this software is not well optimised and might have memory leaks and high CPU usage.
 
Also note that this is an emulator, some assembly instructions might be missing and there are syntactic differences, as an example instead of PUSH, STMDB should be used (all credit to Ardi Vaba). Lauri: PUSH and POP instructions are aliases for LDR and STR instructions, use <code>-fverbose-asm</code> flag and remove optimization flags for GCC to get verbose assembly without aliases.
 
Code example for VisUAL below.
 
<source lang=asm>
; these four lines are the invocation part of the program, lets say an overly simplified variant of the C file that has main function in it
mov r0, #600 ; base value placed in register R0 by the caller
mov r1, #12 ; multiplier value placed in register R1 by the caller
bl multiply ; branch to multiply and store PC+4 in LR or in other words call function multiply
        end                    ; stops the simulator
 
 
 
; the rest is the library portion of the program
multiply
mov r2, #0 ; set result to 0
mov r4, #0 ; set counter to 0
; next five lines are an optimisation for big number multiplication with small numbers
        ; when possible it will decrease the amount of loops we need to preform
cmp r0, r1 ; compare values in R0 and R1
ble multiply_loop  ; if value in R1 is bigger or equal then R0 branch to loop
mov r7, r0          ; otherwise swap arguments
mov r0, r1
mov r1, r7
multiply_loop
cmp r4, r0 ; compare counter to R0
bge multiply_finish ; branch to finish if compare values are grater or equal
add r2, r2, r1 ; add base value to itself and store in R2
add r4, r4, #1 ; increase counter by one
b multiply_loop ; branch to loop, that is - start the loop again
multiply_finish
mov r0, r2 ; store result in R0 as per ARM conventions
        mov    pc, lr          ; same as B LR or in other words return to caller
 
</source>
 
For the libmax.s you can use following snippet to emulate the behaviour of findmax.s
 
<source lang=asm>
; This stuff is so you could run the libmax.s in the ARM simulator
; Don't attempt to compile to assembly the program which has printf and main!
; Instead try to do it from scratch, it should look something like this:
; Since the array can have variable length we can't map them to registers 1:1
; Instead you pass the pointer to the array, that is the memory address of the array's contents
; In the invoking code just use STR instructions to store the numbers in memory
 
mov    r0, r13            ; pass the memory address where you stored the array's contents
mov r1, #20     ; pass the second argument count to the function
 
mov r5, #24              ; my birth date
mov r6, #10              ; my birth month
mov r7, #12              ; current hour
mov r8, #53              ; current minute
mov r9, #25              ; current minute
stmfa r13, {r5,r6,r7,r8,r9}  ; copy register values to memory AND INCREMENT r13 by the number or registers * 4 bytes each
 
; following just illustrates how you handle more numbers
mov r5, #123
mov r6, #624
mov r7, #873
mov r8, #762
mov r9, #543
stmfa r13, {r5,r6,r7,r8,r9}
 
mov r5, #-123
mov r6, #-624
mov r7, #-873
mov r8, #-762
mov r9, #-543
stmfa r13, {r5,r6,r7,r8,r9}
 
mov r5, #54
mov r6, #34
mov r7, #98
mov r8, #76
mov r9, #84
stmfa r13, {r5,r6,r7,r8,r9}
 
bl max        ; place pc+4 into lr and jump to the function max
end                      ; stops the simulator
</source>
 
==Exam question example==
 
Explainations for the mnemonics used in the assembly snippet below:
 
  mov r8, r7      -- Copy value from register r7 to register r8
  add r8, r7, r6  -- Add value of r7 to r6 and store the result in r8
  sub r8, r7, r6  -- Subtract value of r6 from r7 and store the result in r8
  b label          -- *Always* jump to label
  cmp r8, r7      -- Compare r8 and r7 and set flags accordingly
  blt label        -- Jump to label *only* if the leftmost operand is smaller than the rightmost in previously executed cmp
 
Follow the instructions and elaborate what are the values left in the registers after the instruction finishes executing.
 
  magic:
    mov r2, r0            -- Instruction address 0xff00
    mov r3, #0            -- Instruction address 0xff04
  loop:
    cmp r2, r1            -- Instruction address 0xff08
    blt finish            -- Instruction address 0xff10
    add r3, r3, #1        -- Instruction address 0xff14
    sub r2, r2, r1        -- Instruction address 0xff18
    b loop                -- Instruction address 0xff1c
  finish:
    mov r0, r2            -- Instruction address 0xff20
    bx lr                  -- Instruction address 0xff24
 
Fill in the table:
 
{| class="wikitable"
|+Registers
|-
|
|r0
|r1
|r2
|r3
|...
|r13
|r14
|r15
|
|
|-
|Instruction
|a1
|a2
|a3
|a4
|...
|sp
|lr
|pc
|Top of the stack
|What happened?
|-
|bl magic
|39
|17
|?
|?
|...
|0x8f00
|0xfa00
|0xff00
|?
|Function was invoked
|-
|mov r2,r0
|
|
|
|
|...
|
|
|
|
|
|-
|mov r3,#0
|
|
|
|
|...
|
|
|
|
|
|-
|
|
|
|
|
|...
|
|
|
|
|
|-
|
|
|
|
|
|...
|
|
|
|
|
|-
|
|
|
|
|
|...
|
|
|
|
|
|-
|
|
|
|
|
|...
|
|
|
|
|
|-
|
|
|
|
|
|...
|
|
|
|
|
|-
|
|
|
|
|
|...
|
|
|
|
|
|-
|
|
|
|
|
|...
|
|
|
|
|
|-
|
|
|
|
|
|...
|
|
|
|
|
|-
|
|
|
|
|
|...
|
|
|
|
|
|-
|
|
|
|
|
|...
|
|
|
|
|
|-
|
|
|
|
|
|...
|
|
|
|
|
|-
|
|
|
|
|
|...
|
|
|
|
|
|}
 
What does this function do?
 
==Assignment==
 
# Given the function for finding the largest value in an array use ARM cross compiler as shown above to generate the assembly corresponding to the C source code (3p).
# Remove the compiler hints (mostly lines starting with dot), this should leave you less than 20 instructions. If it's more than that try different optimization flags. Analyze and comment the assembly as shown above (3p).
# Use your day of birth, month of birth, current hour, minute, second as arguments as array values and follow the instructions, what values are left in the registers and top of the stack after each instruction finishes?  Use the table from [https://docs.google.com/spreadsheets/d/1H7BtkXRHJb-im29zbTLHIzKKSTbA1Ru8_yg6XwOirCI/edit?usp=sharing here] as a template (9p)
# Optional/extra: Elaborate step by step what happens in the processor: what instruction is being executed and what values are left in registers after instruction finishes? (3p)
# Optional/extra: Recompile with different optimization levels: <code>-Os</code>, <code>-O0</code>, <code>-O1</code>, <code>-O2</code> and <code>-O3</code>. What differences did you notice? (3p)
# Optional/extra: Rewrite the function in assembly with NEON instructions. How much speed boost did you gain? (+10p)
# Optional/extra: What is the space complexity of the algorithm? What is the time complexity of the algorithm? Explain! (+4p)
 
 
 
libmax.c the library portion of the program, or simply put the function definition:
 
  int max(int *m, int count) {
      int largest = -99999;
      int i;
      for (i = 0; i < count; i++) {
          if (m[i] > largest) {
              largest = m[i];
          }
      }
      return largest;
  }
 
findmax.c, the invoking code:
 
  int main() {
      int nums[5] = {1,-3,2,0,5};
      printf("largest number is: %d", max(nums, 5));
  }
 
=Assignment: Debugging ALU design (5p)=
 
Bob had to ship the ALU design to the hardware manufacturer by yesterday. The project manager is angry about missed deadline. Bob still hasn't figured out why the ALU is not working properly. Help Bob by figuring out what's the problem.
 
If you're on Ubuntu 14.04 install GHDL and GtkWave:
 
sudo add-apt-repository ppa:pgavin/ghdl
sudo apt-get update
sudo apt-get install ghdl gtkwave git
 
If you're on Ubuntu 16.04:
 
wget https://launchpad.net/~pgavin/+archive/ubuntu/ghdl/+build/6057094/+files/ghdl_0.31-1pgavin2~trusty2_amd64.deb
wget http://cz.archive.ubuntu.com/ubuntu/pool/universe/g/gnat-4.8/libgnat-4.8_4.8.2-8ubuntu3_amd64.deb
wget http://cz.archive.ubuntu.com/ubuntu/pool/universe/g/gnat-4.8/gnat-4.8-base_4.8.2-8ubuntu3_amd64.deb
sudo dpkg -i libgnat-4.8_4.8.2-8ubuntu3_amd64.deb  gnat-4.8-base_4.8.2-8ubuntu3_amd64.deb  ghdl_0.31-1pgavin2~trusty2_amd64.deb
sudo apt -f install
sudo apt install gtkwave
 
Clone the repository
 
git clone https://github.com/laurivosandi/vhdl-exercise
 
Use <code>make</code> to compile the components and run the testbench:
 
cd path/to/vhdl-exercise
make
 
Expected output of the testbench:
 
alu_testbench.vhd:77:9:@360ns:(report note): Finished testing addition operation of ALU
alu_testbench.vhd:106:9:@1us:(report note): Finished testing subtraction operation of ALU
alu_testbench.vhd:135:9:@1640ns:(report note): Finished testing NAND operation of ALU
alu_testbench.vhd:166:9:@2280ns:(report note): Finished testing NOR operation of ALU
 
However there are few bugs in <code>alu.vhd</code>, find the bugs and correct them. If this is your first experience with VHDL, take a look [http://lauri.vosandi.com/hdl/ here].
 
Assignment steps
 
* Fork the repo on GitHub, clone your repo, fix mistakes, commit and push changes, send link to your GitHub repo where you have fixed the issues (5p)
* Optional/extra: Expand ALU to 8 bits (2p)
* Optional/extra: Add another opcode bit and implement multiplication so opcode(2) high means we're doing multiplication, opcode(1) in case of multiplication selects between higher and lower bits of the result and opcode(0) is used to negate the second operand of multiplication. (+5p)
 
=Assignment: Adders with NAND gates (5p)=
 
In this assignment we use chips to build an adder. Get max points (4p) by wiring up half adder and double your points (8p) if you can make full adder work as well :)
 
[[File:53-7400n-detailed.png]]
 
 
Each CD74HC00E chip contains four NAND gates arranged like this, observe the pit on the left. VCC corresponds to 5V power supply and GND corresponds to ground or 0V.
 
 
==Half adder==
 
Each [http://www.ti.com/lit/gpn/sn74hc03 CD74HC00E] contains 4 CMOS NAND gates.
Use breadboard to wire up half adder with 7400 chips (4p):
 
[[File:Half-adder.png|700px]]
 
The truth table for half adder:
 
  A  |  B  | Cout | Sum  | Human readable (how many bits are high?)
-----+-----+------+------+-------------------------------------------
  0  |  0  |  0  |  0  | 0
-----+-----+------+------+-------------------------------------------
  0  |  1  |  0  |  1  | 1
-----+-----+------+------+-------------------------------------------
  1  |  0  |  0  |  1  | 1
-----+-----+------+------+-------------------------------------------
  1  |  1  |  1  |  0  | 2
 
==Full adder==
 
You can double your points if you manage to wire up a full adder (+5p)
 
[[File:Nand-full-adder.png]]
 
On a breadboard it looks a bit messy, but if you pay attention to gate numbering as shown above it should make your life significantly easier.
The gates 1, 2, 3, 4 are placed in the leftmost chip; 5, 6, 7, 8 are placed in the middle one and gate 9 is the only one in the rightmost chip:
 
 
 
[[File:Full-adder.png|700px]]
 
 
  A  |  B  | Cin | Cout | Sum  | Human readable (how many bits are high?)
-----+-----+-----+------+------+-------------------------------------------
  0  |  0  |  0  |  0  |  0  | 0
-----+-----+-----+------+------+-------------------------------------------
  0  |  0  |  1  |  0  |  1  | 1
-----+-----+-----+------+------+-------------------------------------------
  0  |  1  |  0  |  0  |  1  | 1
-----+-----+-----+------+------+-------------------------------------------
  0  |  1  |  1  |  1  |  0  | 2
-----+-----+-----+------+------+-------------------------------------------
  1  |  0  |  0  |  0  |  1  | 1
-----+-----+-----+------+------+-------------------------------------------
  1  |  0  |  1  |  1  |  0  | 2
-----+-----+-----+------+------+-------------------------------------------
  1  |  1  |  0  |  1  |  0  | 2
-----+-----+-----+------+------+-------------------------------------------
  1  |  1  |  1  |  1  |  1  | 3
 
==Unittest==
 
Use following code snippet on Arduino to test the circuit, it will shuffle through all the possible inputs and test whether the expected output is on the output pins.
If you see <code>sum</code> or <code>cout</code> outputs giving 0 all the time it's likely you have resistors of too low resistance in the circuit which is draining all the voltage on the NAND gate output pins.
 
int PIN_A = 2;
int PIN_B = 3;
int PIN_CIN = 4;
int PIN_SUM = 5;
int PIN_COUT = 6;
void setup() {
    // Set up serial
    Serial.begin(9600);
   
    // Set up operands
    pinMode(PIN_A, OUTPUT);
    pinMode(PIN_B, OUTPUT);
    pinMode(PIN_CIN, OUTPUT);
    digitalWrite(PIN_A, HIGH);
    digitalWrite(PIN_B, HIGH);
    digitalWrite(PIN_CIN, HIGH);
   
    // Set up measurement pins
    pinMode(PIN_SUM, INPUT_PULLUP);
    pinMode(PIN_COUT, INPUT_PULLUP);
}
void test(int a, int b, int cin) {
    int cout = a && b || a && cin || b && cin;
    int sum = a ^ b ^ cin;
   
    digitalWrite(PIN_A, a);
    digitalWrite(PIN_B, b);
    digitalWrite(PIN_CIN, cin);
   
    Serial.print("Writing: a=");
    Serial.print(a);
    Serial.print(", b=");
    Serial.print(b);
    Serial.print(", cin=");
    Serial.print(cin);
    Serial.print("  Expecting: sum=");
    Serial.print(sum);
    Serial.print(", cout=");
    Serial.print(cout);
   
    int measured_sum = digitalRead(PIN_SUM);
    int measured_cout = digitalRead(PIN_COUT);
    Serial.print("  Got: sum=");
    Serial.print(measured_sum);
    Serial.print(" cout=");
    Serial.print(measured_cout);
   
    Serial.print("  Test:");
    if (measured_sum != sum || measured_cout != cout) {
        Serial.print("  FAIL");
    } else {
        Serial.print("  GOOD");
    }
    Serial.println();
    delay(500);
}
void loop() {
    Serial.println("Running test");
    test(0, 0, 0);
    test(0, 0, 1);
    test(0, 1, 0);
    test(0, 1, 1);
    test(1, 0, 0);
    test(1, 0, 1);
    test(1, 1, 0);
    test(1, 1, 1);
}
 
=Deprecated assignment: Investigating microcontrollers=
 
In the IoT assignment we already played around with ESP32 microcontroller, most microcontroller concepts are the same for Arduino (interrupts, PWM, etc).
 
In this assignment we use Arduino to implement traffic lights for [http://www.nobleled.com/uploads/110833/100_101.jpg crossroads].
If you don't care much about doing this assignment hands on, you can give a try [https://123d.circuits.io/ online].
 
==Assignment steps==
 
* Install Arduino IDE on your personal machine, on Windows/Mac check out [https://www.arduino.cc/ Arduino homepage] for instructions, on Ubuntu <code>apt install arduino</code> should suffice. You can also use school computers, Ubuntu has Arduino preinstalled.
* If you happen to have Java 9 running by default, check with <code>java -version</code> then you might have to switch to Java 8 instead: <code>sudo apt install openjdk-8-jdk; sudo update-java-alternatives -s java-1.8.0-openjdk-amd64</code>
* From Arduino IDE menu open up File -> Examples -> 01. Basic -> Blink
* In Arduino IDE menu Tools -> Board -> Arduino Mega2560
* In Arduino IDE menu Tools -> Serial port -> Select what makes sense
* On Ubuntu you may have to add yourself to the dialout group before you can access the serial ports. Use the command <code>sudo gpasswd -a $USER dialout</code>, log out and log in again to your PC.
* Press the second button in the toolbar to upload the code, make sure the changes take effect on the board. You should see on-board LED blinking.
* Connect some LED-s to the board. '''MAKE SURE YOU USE RESISTORS TO PROTECT LED-s AND THE BOARD'''
* Implement traffic light cycle using <code>digitalWrite</code> and <code>delay</code> in the <code>loop</code> function (3p)
* Use interrupts to read button presses, see example below. Pressing the button should toggle the LED now (1p)
* Extra: Use [https://www.arduino.cc/en/Reference/AnalogWrite PWM] to implement smoother blinking (1p)
* Extra: Use [http://lauri.vosandi.com/arduino/lcd1602-key-shield.html LCD1602] shield to display countdown (1p)
* Use [https://www.arduino.cc/en/Reference/Serial serial] interface to send messages to your PC.
* You can find useful code snippets for Robotics club equipment [http://lauri.vosandi.com/arduino here]
 
==Code example: reading button press with interrupts==
 
Set up a push button as shown below:
 
[[File:Arduino-pushbutton_bb.png|500px]]
 
Insert following code to Arduino IDE and upload it to Arduino, verify that it works:
 
int led = 13;
int button  = 2;
int flag = LOW;
void setup() {
    pinMode(led, OUTPUT);        // Set up pin 13 as digital output
    pinMode(button, INPUT_PULLUP);      // Set up pin 9 as digital input with pull-up resistor enabled
    // Associate interrupt handler with an event
    attachInterrupt(digitalPinToInterrupt(button), onButtonPressed, FALLING);
}
void onButtonPressed() {
    // Keep this as short as possible
    flag = HIGH;
}
void loop() {
    // Do something useful or let processor sleep here
    if (flag) {
        digitalWrite(led, HIGH);
        delay(2000);
        flag = LOW;
    }
    digitalWrite(led, LOW);
    delay(50);
}
 
==Code example: Cycling through different traffic light states==


In this assignment we use Arduino:
Add LED-s to the design:


* Install Arduino IDE on your personal machine.
[[File:Traffic-lights_bb.png|500px]]
* Use analog input to measure distance using [http://lauri.vosandi.com/arduino/proximity-sensor.html proximity sensor] and map it to hue.
* Use Atmega built-in functionality for [http://lauri.vosandi.com/arduino/pwm-simulator.html generating PWM] signals to drive RGB LED.
* Use [http://lauri.vosandi.com/arduino/lcd1602-key-shield.html LCD1602] shield to display current values.
* Use [http://lauri.vosandi.com/arduino/interrupts.html interrupts] to read button presses


Use following as hint to implement cycling through traffic light states:


=Assignment: Designing arithmetic-logic unit=
// Define pin numbers
int car_green = 5;
int car_yellow = 6;
int car_red = 7;
// For LED-s we're sinking the 5V voltage, hence LED is turned on when voltage on the pin is LOW
int on = LOW;
int off = HIGH;
void setup() {
    pinMode(car_green, OUTPUT);
    pinMode(car_yellow, OUTPUT);
    pinMode(car_red, OUTPUT);
    digitalWrite(cars_yellow, off);
    Serial.begin(9600);
}
void loop() {
    Serial.println("Cars green, pedestrians red for 5 seconds");
    digitalWrite(cars_red, off);
    digitalWrite(cars_green, on);
    delay(5000);
    Serial.println("Green for cars is blinking 5 seconds");
    for(int j = 0; j < 10; j++) {
        digitalWrite(cars_green, j % 2 == 0);
        delay(500);
    }
    Serial.println("Cars red, pedestrians green for 5 seconds");
    digitalWrite(cars_green, off);
    digitalWrite(cars_red, on);
    delay(5000);
}


Deadline: 18. November


In this assignment we use SN7400 chips to build a 4-bit arithmetic-logic unit. Form teams of 6. You'll be given 3x breadboards, 30x SN7400 chips, jumper cables, slide switches, LED-s, resistors. Use: breadboard to connect the components; four slide switches for operand A; four slide switches for operand B; four slide switches to select ALU operation mode and four LED-s as outputs. '''Make sure you protect LED-s with resistors'''.
==Skeleton of the final submission==


Deadline: TBD
The real traffic light has of course a little bit more states:


Points: 4 points
1. Cars green, pedestrians red for 10 seconds
2. Cars green **blinking**, pedestrians red for 2 seconds
3. Cars red, pedestrians green for 10 seconds
4. Cars red, pedestrians green **blinking** for 2 seconds
5. Cars yellow, pedestrians red for 2 seconds


Assignment steps:
So your final code will look something like this:


* Considering 5V power supply, voltage drop of 1.8V on LED and maximum current of 20mA what is the minimum resistance for the protective resistor?
// Define pin numbers here
* Each SN7400 contains four NAND gates. Use two SN7400-s to compose a full adder. Use four [http://students.cs.tamu.edu/tanzir/csce350/handout/lab6.html full adders] to compose 4-bit carry-ripple adder. Verify your adder works.
* Add switch for toggling between adding/subtracting, you can achieve this by inverting bits of operand B and carry in. Verify that addition and subtraction works as expected.
// Initially the flag is low
* Add circuitry for calculating [https://en.wikipedia.org/wiki/Bitwise_operation#Bitwise_operators bitwise] NAND and NOR operations on operand A and operand B.
int pedestrian_requested_crossing = LOW;
* Add 2:1 muxes to select between NAND and NOR.
 
* Add 2:1 muxes to select between NAND/NOR output and adder/subtractor output.
void setup() {
* Evaluate the cost of your design? Evaluate the energy consumption of your design?
    // Initialize necessary pins as digital outputs
* Document your design as VHDL or Verilog file, upload it to GitHub.
    // Reset pins with digitalWrite
Present your solution in Tuesday session.
    // Initialize button pin as digital input
    // Enable pull-up resistor on button pin
    // Associate button interrupt handler function (onButtonPressed) below with the event on the button
}
void onButtonPressed() {
    // Here set pedestrian_requested_crossing flag to HIGH
    // Don't do anything else here!
}
void loop() {
    Serial.println("Cars green, pedestrians red for 5 seconds");
    digitalWrite(cars_red, off);
    digitalWrite(cars_green, on);
    // Set pedestrian lights here
    delay(5000);
    if (pedestrian_requested_crossing) {
        Serial.println("Green for cars is blinking 5 seconds");
        for(int j = 0; j < 10; j++) {
            digitalWrite(cars_green, j % 2 == 0);
            delay(500);
        }
        Serial.println("Cars red, pedestrians green for 5 seconds");
        digitalWrite(cars_green, off);
        digitalWrite(cars_red, on);
        // Set pedestrian lights here
        delay(5000);
        // Add another state here
        // And another state here
        // Reset flag here
    }
}


=Assignment: Licensing your work=
=Deprecated assignment: Publishing your work=


Deadline: 25. November
In the Git assignment most of the following was already required


* Add authorship information to your GitHub repositories
* Clean up your Git repositories and send the URL of a repository that you've actively worked on (for example the one from Java course) to Lauri, but before check the following:
* Add [http://opensource.org/licenses LICENSE] file in the repository directory and commit the changes.
** The repository should show changes you've made over at least few days.
** The repository has to contain only plaintext files unless there is a really good reason not to (eg. images).
** There has to be authorship information in the files.
** Add <code>.gitignore</code> file to your GitHub repositories to ignore temporary files, see [https://git-scm.com/docs/gitignore Git docs] for more details.
** Add [http://opensource.org/licenses LICENSE] file in the repository directory and commit the changes. LICENSE should answer to some important questions
*** Who is the copyright holder?
*** Under which conditions is redistribution permitted?
** Add nicely formatted README file, see [https://help.github.com/articles/markdown-basics/ Markdown] (online editor [http://dillinger.io here]) and [http://sphinx-doc.org/rest.html reStructuredText] for more details. README should answer to several silly questions:
*** What is the repository about?
*** Who made it?
*** How can the author be contacted? E-mail address, IRC chat channel on Freenode, Skype username?
*** How can the code be used? What hardware is necessary? How should the user wire the circuit to make it work? Add images if necessary.
*** What is the policy for including changes from third party developers?
* There will be more details here


=Extra points=
=Extra points=

Latest revision as of 10:55, 12 December 2017

In this course we'll give an introduction to variety of topics from hardware to software. The course will follow roughly the same structure as Computer Science 101 at Stanford University with more hands-on approach. This course substitutes Estonian courses I100 Sissejuhatus informaatikasse and I201 Arvutid.

Feedback form: https://goo.gl/forms/evqt69NeRkf5IPKx1

Video recordings: 2016 fall, 2015 fall.

Course credits: 6 ECTS

Lecturer: Lauri Võsandi

E-mail: lauri [donut] vosandi [plus] i600 [ät] gmail [dotchka] com


Grading

Grade mapping:

  • 0-50 points, fail
  • 51-60 points, pass 1
  • 61-70 points, satisfactory 2
  • 71-80 points, average 3
  • 81-90 points, good 4
  • 91-100 points, very good 5

Grading is split between theory and practice

  • Exam of 50 points, preparation in lectures and general discussion in workshops.
  • Assignments, see instructions and points below.
  • Extra points for improving quality of wiki articles, extracurricular work and also helping others, see points below.

Exam will be halfway between oral and written: You'll be given 30 minutes to prepare for several random questions and 15 minutes to discuss what you've answered. If you don't have prior experience with the topics, it's highly recommended to take part of the sessions in order to pass the exam. You can use a calculator and this wiki page, don't rely on the access to terminal or other software tools. You can not access Internet or any remote computers.

Exam sections:

  • Execution of ~10 processor instructions
  • Bunch of randomly selected questions from this page
  • TBD

Lecture: Computer hardware

Jargon: CPU, RAM, ROM, HDD, SSD, PCI, PCI Express, USB 2.0, USB 3.0, VGA, HDMI, DVI, LCD, TFT, LED, OLED, AMOLED, CRT, PWM

Lecture slides Random access memory, permanent storage, buses, input devices, display technologies, networking

Potential exam questions:

Security section: Hacking SD cards

Homework:

Assignments:

  1. test

Lecture: Storage abstractions

In this lecture we'll talk about permanent storage abstractions: block device, disk, partition, file system, directory/folder, file, journaling, FAT32, NTFS, ext4, HFS+

Lecture slides

Lecture: Bootloaders, kernels

In this lecture we'll discuss how a computer boots and how an operating system kernel is loaded.


Lecture slides: bootloaders, kernels.

Jargon: BIOS (basic input/output system), UEFI (Unified Extensible Firmware Interface), bootloader, kernel, process, context switch, x86 real/protected mode, paged virtual memory, swap/pagefile, kernelspace/supervisor mode, userspace, hypervisor mode (ring -1), system management mode aka (ring -2).

Potential exam questions:

  • What is the role of BIOS/UEFI in x86-based machines?
  • Explain step by step how operating system is booted up, see slides for flowchart.
  • Describe the functionality provided by general purpose operating system. See architecture of Windows NT, Android, OS X.
  • What are the main differences between real mode and protected mode of x86-based processor?
  • What happens during context switch?
  • What is the purpose of paged virtual memory?


Security section: DMA attack, editing GRUB entries to gain root shell without password prompt, 20 years old security exploit in x86 processors.

Lecture: Libraries, frameworks

Lecture slides

Jargon: framework, library, ABI, API

Security section: HTTP.sys bug crashes IIS web servers, OpenSSL bug Heartbleed.

Lecture: Programming languages

In this lecture we'll talk about programming languages

Lecture slides: programming languages, stack machine

Jargon: stack machine, lexer/lexeme, token, abstract syntax tree

Potential exam questions:

Assignments:

Lecture: Data encoding

Lecture slides

In this lecture we'll talk about bits, bytes, integers, strings, pixels, audio encodings, video encoding etc.

Potential exam questions:

  • What is bit? Nibble? Byte? Word?
  • Write 9375 in binary, hexadecimal?
  • Write 0xDEADBEEF in decimal?
  • How are dates/time stored in binary?
  • What is quantization in terms of signal processing?
  • How are integers stored in binary? What integer range can be described using n bits? How many bits are required to describe integer range from n .. m.
  • How are single precision and double precision floating point numbers stored in binary according to IEEE754 standard? Floating-point multiplication
  • What is the difference between CMYK and RGB color models? How are YUV, HSV and HSL colorspaces related to RGB? What are sRGB and YCbCr and where are they used?
  • How is data encoded on audio CD-s? What is the capacity of an audio CD?
  • What is sampling rate? What is bit depth? What is resolution?
  • What is bitrate?
  • What is lossy/lossless compression?
  • What is JPEG suitable for? Is JPEG lossy or lossless compression method?
  • What is PNG suitable for? Does PNG support compression?
  • How are time domain and frequency domain related in terms of signal processing? What is Fourier transform and where it is applied?

Jargon: 44.1kHz sampling rate, 16-bit audio, RGB565 pixel format, RGB888 pixel format

Relevant assignments:

Lecture: Code execution in processor

Lecture slides

It's highly reccommended to play around with simulators here is one for MIPS processors.

In this lecture we'll talk about different processor architectures, instructions, pipelining, function calls, jumps, loops etc.

Assignments:

Exam question:

  • Given ~10 instructions and their explainations, follow the instructions and elaborate after every step what happened in the processor?


Lecture: Introduction to Boole algebra

Lecture slides

Exam questions:

  • Simplify A AND A OR B
  • Show addition of X and Y in binary
  • Show subtraction of X and Y in binary
  • Show multiplication of X and Y in binary


Assignments:

Lecture: Hardware description languages

Lecture slides

Potential exam questions:

  • What are the uses for hardware description languages?
  • What is latch?
  • What is flip-flop?
  • What is mux (multiplexer)?
  • What is register? Register file?
  • What is ALU?
  • What is floating-point unit?
  • What is a cache?
  • What is a bus?
  • Show the circuit diagram for A OR B AND C, NOT A AND B, <insert some other Boole formula here>?
  • Show the truth table for <insert Boole formula here>?
  • Write the equivalent Boole formula of a circuit diagram.


Lecture: Publishing work

Lecture slides

Potential exam questions:

  • What are the major implications of MIT, BSD and GPL licenses?
  • What are the differences between copyright, trademark, trade secret?
  • Where would you use waterfall software development model? Where would you use agile?
  • What is the purpose of a version control system?
  • What would you store in a version control system?

Lecture: Algorithms and data structures

Lecture slides

Lecture slides (shortened)

Potential exam questions:

  • What is time complexity of algorithm?
  • What is space complexity of algorithm?
  • What's a good algorithm?

Lecture: History

Topics: Computer history, Silicon Valley, standards

Lecture slides

Potential example questions:

  • What is Moore's law? What is Rock's law?
  • What were the major contributing factors for success of Microsoft, Apple, Google, <your favourite company>?
  • What were the major contributing factors to the success of Silicon Valley?

Lecture: Microcontrollers

Lecture slides

Jargon: microprocessor, microcontroller, coprocessor, floating-point unit (FPU), digital signal processor (DSP), field programmable grid array (FPGA), sensors, actuators, digital input/output, analog input, general purpose input/output (GPIO), interrupt, interrupt handler, timer/counter, pulse-width modulation (PWM),

Potential exam question:

  • What distinguishes microcontroller from microprocessor?
  • What are the differences between Hardvard architecture and von Neumann architecture?
  • What is an interrupt?
  • What is an timer?


Assignment: Investigating PC hardware (5p)

Goal: Get familiar with your laptop hardware. Get out of the comfort zone of your primary operating system and try out other operating systems. Learn about hardware virtualization.

Deadline: 26. September

Use what you learned in Getting started with Ubuntu workshop:

  • Read the instructions before you act.
  • Place your preferred ISO image to a memory stick using dd or Win32 Disk Imager and boot it on your personal laptop. You do not need to install Ubuntu on your harddisk, simply click Try Ubuntu once the operating system boots off the memory stick and carry out following tasks.
  • Open up terminal by pressing Ctrl-Alt-T.
  • Use lsb_release -a to check what operating system distribution you're running, try apt install lsb-release if command is not available.
  • Use uname -sr to check what operating system kernel you're running.
  • Use cat /proc/cpuinfo and check processor information. What CPU model and how many cores your computer has?
  • Use arch to check what CPU architecture is being used by the operating system. Is it 32-bit or 64-bit?
  • Use cat /proc/meminfo to check memory usage. How much RAM your computer has?
  • Use lspci -t -v -nn to enumerate PCI and PCI Express devices, attempt to identify what is what, try apt install pciutils if command is not available.
  • Use lsusb and lsusb -t to enumerate USB devices, again attempt to identify what is what, try apt install usbutils if command is not available.
  • Use sudo fdisk -l to enumerate disks and partitions. How big is your harddisk? How many and how big partitions it has?
  • Use lsblk to enumerate block devices.
  • Use xrandr to enumerate display outputs. What video output resolutions are available and which one is currently used?
  • Use cat /proc/asound/cards to check which audio devices are available.
  • Use dmidecode to see even more information about your computer.
  • Use ifconfig -a or ip addr list to list all network interfaces.
  • Use iwconfig or iw list to list all wireless network interfaces. Is your wireless network interface detected? If not take a guess why?
  • Use hcitool dev to list bluetooth host controller. Is your bluetooth device detected?
  • Use glxinfo to check what 3D rendering capabilities are available, is it hardware accelerated? (hint: is direct rendering enabled?)
  • What audio card is your machine using? What bus is it using?
  • What graphics controller is your machine using? What bus is it using?
  • What webcam is your machine using? What bus is it using?
  • What wired network chipset your computer has? What bus is it using?
  • What wireless network chipset your computer has? What bus is it using?
  • What Bluetooth device your computer has? What bus is it using?
  • Is there a cellular modem connected and how it's connected?
  • Boot the ISO image in VirtualBox and follow the same steps as above, what are the differences and why?
  • Answer to questions above and send it as plain text e-mail to Lauri, make sure you use the address supplied above with the course code, otherwise your mail is not searchable in my mailbox. Attach collected command outputs as plain text file, do not send .odt, .doc files, these are not readable on my smartphone. When answering to questions phrase the text in a way that is understandable out of context, so I don't have to open up wiki to understand what you're talking about.

Note that I can't expect you to install Ubuntu on your physical machine, but I can help if you want to do that. You should have Ubuntu ready to go in a virtual machine at least for subsequent sessions. We're using Ubuntu because it's widely used on servers and in the cloud, so any Ubuntu skills will definitely come handy in future. If you're already familiar with Linux, it's suggested to take a look at other interesting operating systems such as FreeBSD or OpenBSD. Take a look at Kali Linux if you're interested in penetration testing.

Assignment: Investigating embedded hardware (5p)

Background: Most of the smartphones nowadays are using SoC built around ARM processor. Raspberry Pi is an excellent piece of hardware to for checking out how an ARM-based machine looks like.

Deadline: 3. October

Goal: Get familiar with hardware ARM-based hardware.

Use what you learned in Getting started with Ubuntu and Getting started with Raspberry Pi workshops:

  • Read the instructions before you get busy.
  • Boot Raspbian on Raspberry Pi.
  • Use the commands described in previous assignment to examine the environment of Raspbian on Raspberry Pi. What are the major differences compares to your laptop and virtual machine?
  • What buses is Raspberry Pi making use of?
  • What filesystems is Raspbian making use of?
  • Answer to questions above and send it as e-mail to Lauri, make sure you use the address supplied above with the course code, otherwise your mail is not searchable in my mailbox. Attach collected command outputs as plain text file, do not send .odt, .doc files, these are not readable on a phone. When answering to questions phrase the text in a way that is understandable out of context, so it is not necessary to open up wiki to understand what you're talking about.

Assignment: Investigating LAMP (5p)

LAMP software bundle is consists of Linux-based OS, Apache web server, PHP programming language runtime and MySQL database. Most of the web applications on the Internet including Facebook are built on top of LAMP-styled software stack. Use a Ubuntu server virtual machine to set up WordPress, ownCloud or any well-known web application that makes use of a database such as MySQL. To make your life easier also set up SSH public key authentication.

Goal: Get familiar with LAMP stack. Get comfortable with (SSH) public key authentication.

Tasks:

  • Install Ubuntu 16.04 server in a virtual machine or use prepared virtual appliance
  • Configure second network interface as Host-only adapter, this is already done in case of OVA
  • Use SSH to connect to your server over the network. If you're using Windows on your laptop use PuTTY to gain access to command line and WinSCP to copy files, otherwise simply boot Ubuntu in a virtual machine and use ssh username@hostname to invoke commands and sftp://username@hostname in the file browser to access filesystem.
  • Install webserver, MySQL server and PHP, this is already done in case of OVA. Usually you'd do something like apt install apache2 mysql-server php7.0 phpmyadmin
  • Set up any of the web applications referenced above in /var/www/html. You may be interested in reading also this and this.
  • Demonstrate that the web application you installed works in next lab, screenshots/photos with explanation sent to e-mail above also suffice.
  • Optional: Set up SSH public key authentication to enos.itcollege.ee.
  • Optional: Set up public key authentication to your Ubuntu box. Windows users might want to take a look at PuTTYgen instructions.

Note: If you're maintaining similar installation of a production server that is accepted as well. WAMP on Windows is also accepted.

Assignment: Set up basic IoT scenario (5p)

NB! This year ESP32 instead of Raspberry Pi, see instructions here

Internet of Things is one of the emerging technologies (read: hype). IoT is essentially about getting everything online, including lightbulbs, switches, window shades etc. In this assignment LED symbolizes a light and the task is to implement code which allows user to switch the LED on and off over the network.

Goal: Get familiar how Python code can be started up. Build basic IoT appliance, a light that can be turned on and off via the Internet.

  • Complete the Python track at CodeAcademy if you haven't done that yet.
  • Follow the wiki page Blinking LED section under Getting started with Raspberry Pi.
  • Get an LED blinking on command-line.
  • Get LED blinking from Python code.
  • Get basic HTTP server running in Python.
  • Combine all of the above, build an HTTP server that can be used to turn LED on and off from via web browser.
  • Optional: Take a peek at next assignment and upload working version to GitHub.
  • Optional: Smoothen the transitions using PWM.

Assignment: Collaborating using Git (5p)

Software development is usually done by several contributors, to facilitate efficient collaboration a distributed version control system is a must. In this assignment you'll upload your code to GitHub and modify fellow student's code to reflect changes in the Raspberry Pi setup:

Goal: Get familiar with distributed version control systems. Collaborate.

  • Create GitHub account if you haven't done so yet.
  • Create a Git repository, eg http://github.com/your-username/rpi-iot-example
  • Install Git on your Raspberry Pi by using apt-get install git.
  • Clone the repository to your Raspberry Pi using git clone http://github.com/your-username/rpi-iot-example.
  • Move the Python code created earlier to the Git repository directory.
  • Use git add to add the files.
  • Configure full name: git config --global user.name "Firstname Surname"
  • Configure e-mail: git config --global user.email first.last@domain.tld
  • Use git commit to create the initial commit.
  • Use git push to push the commits to GitHub server.
  • Set up public key authentication between your laptop and GitHub servers, verify that https://github.com/username.keys gives the expected result.
  • Use git clone to clone the repository to your laptop. Ubuntu should first apt-get install git, Windows and Mac users might want to take a look at Git homepage. If you're looking for graphical user interface take a look at GitHub Desktop or TortoiseGit.
  • Create README in the repository directory, populate it with relevant content - what is it about, who made it etc and commit the changes.
  • In your Raspberry Pi setup RGB LED should be connected as shown above.
  • Clone fellow student's repository and adapt the code to reflect physical changes to the setup, the Python snippet should now permit changing the color of the light, have it blinking and turn it off. Other interesting modes are awarded with extra points, eg use HTML5 input type range.
  • Use either Atlassian Tutorials, git - the simple guide or Try Git as a reference if you get lost.
  • Document in the README what GPIO pins are used in the code for red, green and blue. Extra points for making the Python code configurable from command-line.
  • Late submissions will heavily lose points for failure to comply with decent commit messages.
  • Send Lauri the URL of your repository at GitHub.

Git resources: Pro Git Book (read the basics to get kickstarted) and Codecademy Git course.

Assignment: Investigating compilers (15p)

Use what you learned in the Getting started with GCC session. See assignment steps below. Everything you need to understand ARM instructions should be here. Another good resource seems to be Introduction to ARM by David Thomas. See page 14 of Procedure Call Standard for the ARM® Architecture if you want to learn more about how function calls are implemented on ARM. See here for more information about branching instructions: B, BL, BX. <Insert extemely useful link here yourself>

Example

If you don't have Ubuntu installed on your machine skip next step and use SSH to perform these steps remotely at enos.itcollege.ee. Otherwise install ARM cross-compiler:

apt-get install gcc-arm-linux-gnueabi

Consider C snippet for calculating integer exponentiation, place it in a file power.c:

int power(long base, long exponent) {
    int counter;
    int result = 1;
    for (counter = 0; counter < exponent; counter++)
        result *= base;
    return result;
}

Generating assembly

Compile ARM assembly of the code snippet:

arm-linux-gnueabi-gcc -Os -S power.c -o power.s   # Cross-compile ARM assembly file power.s from C source code file power.c
cat power.s                                       # Dump the assembly file on terminal

The compiler should produce something similar to following:

	.arch armv5t
	.fpu softvfp
	.eabi_attribute 20, 1
	.eabi_attribute 21, 1
	.eabi_attribute 23, 3
	.eabi_attribute 24, 1
	.eabi_attribute 25, 1
	.eabi_attribute 26, 2
	.eabi_attribute 30, 2
	.eabi_attribute 34, 0
	.eabi_attribute 18, 4
	.file	"power.c"
	.text
	.align	2
	.global	power
	.type	power, %function
power:
	@ args = 0, pretend = 0, frame = 0
	@ frame_needed = 0, uses_anonymous_args = 0
	@ link register save eliminated.
	cmp	r1, #0
	mov	r2, #1
	ble	.L2
	mov	r3, #0
.L3:
	add	r3, r3, #1
	cmp	r3, r1
	mul	r2, r0, r2
	bne	.L3
.L2:
	mov	r0, r2
	bx	lr
	.size	power, .-power
	.ident	"GCC: (Ubuntu/Linaro 4.7.3-12ubuntu1) 4.7.3"
	.section	.note.GNU-stack,"",%progbits

Commenting the assembly

You can safely ignore the lines starting with dot, these are simply hints for the next stage of the compilation process. Thus you're left only (!!!) 10 actual instructions. Here you can also see what kind of comments are expected from the submission:

power:
	cmp	r1, #0             /* Compare register r1 (second argument) contents against constant 0 */    
	mov	r2, #1             /* Copy the constant 1 to register r2 */
	ble	.L2                /* Jump to label .L2 if the comparison was false */
	mov	r3, #0             /* Copy the constant 0 to register r3 */
.L3:
	add	r3, r3, #1         /* Perform r3 = r3 + 1 */
	cmp	r3, r1             /* Compare register r3 and r1 (second argument) */
	mul	r2, r0, r2         /* Perform r2 = r0 (first argument) * r2 */
	bne	.L3                /* If r3 and r1 were different jump to label .L3
.L2:
	mov	r0, r2             /* Copy register r2 contents to register r0 */
	bx	lr                 /* Jump back to caller */


Analyzing the code

Code analysis reveals that registers are mapped as following:

  • r0 - argument base and eventually return value
  • r1 - argument exponent
  • r2 - variable result
  • r3 - variable counter


We can also see how the for loop gets translated into assembly:

power:
	cmp	r1, #0
	mov	r2, #1
	ble	.L2
	mov	r3, #0
.L3:
	add	r3, r3, #1 <---+
	cmp	r3, r1         | 4 instructions are executed repeatedly
	mul	r2, r0, r2     | if exponent is greater than 0
	bne	.L3 -----------+
.L2:
	mov	r0, r2
	bx	lr

Stepping through the instructions

When we execute the function with for example arguments base=5 and exponent=3 following happens in the processor. Note that this will essentially calculates 5 ^ 3 (5 to the power of 3) which is 125. Following has been done manually to illustrate how much time does it take to execute the the function.


/* When processor enters the function body the arguments are already placed in registers r0=5 (base), r1=3 (exponent) */
cmp r1, #0                        /* Compare exponent to 0 */
mov r2, #1                        /* Place constant 1 in register r2,
                                     this corresponds to result = 1 in C code */
ble .L2                           /* Exponent was not less than 0, so no jump to L2
mov r3, #0                        /* Place constant 0 in register r3,
                                     this corresponds to variable counter */

add r3, r3, #1                    /* Perform r3 = 0 + 1 which results in 1 being stored to r3
                                     this corresponds to first invocation of counter++ in C code */
cmp r3, r1                        /* Compare counter (1 in this case) to exponent (3), this will be used by bne instruction below */
mul r2, r0, r2                    /* Perform r2 = r0 * r2 which results in 1 * 5 = 5 being placed in r2
                                     this corresponds to first invocation of result *= base in C code */
bne .L3                           /* The comparison resulted in counter being not equal to exponent, so we jump back to L3
                                     this corresponds to first invocation of counter < exponent in C code */

add r3, r3, #1                    /* Perform r3 = 1 + 1 which results in 2 being stored to r3
                                     this corresponds to second invocation of counter++ in C code */
cmp r3, r1                        /* Compare counter (2 in this case) to exponent (3), this will be used by bne instruction below */
mul r2, r0, r2                    /* Perform r2 = r0 * r2 which results 5 * 5 = 25 being placed in r2
                                     this corresponds to second invocation of result *= base in C code */
bne .L3                           /* The comparison resulted in counter being not equal to exponent, so we jump back to L3
                                     this corresponds to second invocation of counter < exponent in C code */

add r3, r3, #1                    /* Perform r3 = 2 + 1 which results in 3 being stored to r3
                                     this corresponds to third invocation of counter++ in C code */
cmp r3, r1                        /* Compare counter (3 in this case) to exponent (3), this will be used by bne instruction below */
mul r2, r0, r2                    /* Perform r2 = r0 * r2 which results 25 * 5 = 125 being placed in r2
                                     this corresponds to third invocation of result *= base in C code */
bne .L3                           /* The comparison resulted in counter being equal to exponent, so we DO NOT jump back to L3 */

mov	r0, r2                     /* Copy register r2 contents (125) to register r0 */
bx	lr                         /* Jump back to caller */
/* Function returns with 125 placed in r0 this is where caller function should expect the return value */
/* The other registers will still hold whatever values were left there: r1 = 3, r2 = 125, r3 = 3 */

ARM registers

If you get lost with the ARM register naming conventions use following as a guide.

ARM7 has 16x 32-bit registers:

 r0 (a1) - Argument/result/scratch register, this is where first function argument is usually placed
 r1 (a2) - Argument/result/scratch register, this is where second function argument is placed
 r2 (a3) - Argument/scratch register 3
 r3 (a4) - Argument/scratch register 4
 r4 (v1) - Variable register 1
 r5 (v2) - Variable register 2
 r6 (v3) - Variable register 3
 r7 (v4) - Variable register 4
 r8 (v5) - Variable register 5
 r9 (v6) - Variable register 6
r10 (v7) - Variable register 7
r11 (fp) - Frame pointer, variable register 8 
r12 (ip) - Intra-procedure call scratch register
r13 (sp) - Stack pointer
r14 (lr) - Link register (to which memory address currently
                          executing function should return to?)
r15 (pc) - Program counter (what is the memory address of instruction
     ^                      that we are going to execute next?)
     |
     +- These are simply aliases for the numbered registers and
        can be used interchangeably.

Another example

For learning purposes try to do the steps described above for the different flavours of Fibonacci calculation function below:


Fibonacci calculation using for-loop:

int fib(int n) {
    int a = 1;
    int b = 1;
    int i;
    for (i = 0; i < n; i++) {
        int s = a + b;
        a = b;
        b = s;
    }
    return a;
}

Fibonacci calculation using recursion:

int fib2(int n) {
    if (n == 1 || n== 2) {
        return 1;
    } else {
        return fib(n-1) + fib(n-2);
    }
}

Fibonacci calculation using while-loop:

int fib4(int n) {
    int a = 1;
    int b = 1;
    int i = 0;
    while(1) {
        i += 1;
        if (i >= n) { break; }
        int s = a + b;
        a = b;
        b = s;
    }
    return a;
}


Working with VisUAL

Software itself you will find at VisUAL - A highly visual ARM emulator

Be aware, this software is not well optimised and might have memory leaks and high CPU usage.

Also note that this is an emulator, some assembly instructions might be missing and there are syntactic differences, as an example instead of PUSH, STMDB should be used (all credit to Ardi Vaba). Lauri: PUSH and POP instructions are aliases for LDR and STR instructions, use -fverbose-asm flag and remove optimization flags for GCC to get verbose assembly without aliases.

Code example for VisUAL below.

; these four lines are the invocation part of the program, lets say an overly simplified variant of the C file that has main function in it
	mov	r0, #600	; base value placed in register R0 by the caller
	mov	r1, #12 	; multiplier value placed in register R1 by the caller
	bl	multiply	; branch to multiply and store PC+4 in LR or in other words call function multiply
        end                     ; stops the simulator



; the rest is the library portion of the program
multiply
	mov	r2, #0 		; set result to 0
	mov	r4, #0 		; set counter to 0
	; next five lines are an optimisation for big number multiplication with small numbers
        ; when possible it will decrease the amount of loops we need to preform
	cmp	r0, r1 		; compare values in R0 and R1
	ble	multiply_loop   ; if value in R1 is bigger or equal then R0 branch to loop
	mov	r7, r0          ; otherwise swap arguments
	mov	r0, r1
	mov	r1, r7
multiply_loop
	cmp	r4, r0 		; compare counter to R0
	bge	multiply_finish ; branch to finish if compare values are grater or equal
	add	r2, r2, r1 	; add base value to itself and store in R2
	add	r4, r4, #1 	; increase counter by one
	b	multiply_loop	; branch to loop, that is - start the loop again	
multiply_finish
	mov	r0, r2 		; store result in R0 as per ARM conventions
        mov     pc, lr          ; same as B LR or in other words return to caller

For the libmax.s you can use following snippet to emulate the behaviour of findmax.s

; This stuff is so you could run the libmax.s in the ARM simulator
; Don't attempt to compile to assembly the program which has printf and main!
; Instead try to do it from scratch, it should look something like this:
; Since the array can have variable length we can't map them to registers 1:1
; Instead you pass the pointer to the array, that is the memory address of the array's contents
; In the invoking code just use STR instructions to store the numbers in memory

mov     r0, r13            ; pass the memory address where you stored the array's contents
mov	r1, #20	   	  ; pass the second argument count to the function

mov r5, #24               ; my birth date
mov r6, #10               ; my birth month
mov r7, #12               ; current hour
mov r8, #53               ; current minute
mov r9, #25               ; current minute
stmfa r13, {r5,r6,r7,r8,r9}  ; copy register values to memory AND INCREMENT r13 by the number or registers * 4 bytes each

; following just illustrates how you handle more numbers
mov r5, #123
mov r6, #624
mov r7, #873
mov r8, #762
mov r9, #543
stmfa r13, {r5,r6,r7,r8,r9}

mov r5, #-123
mov r6, #-624
mov r7, #-873
mov r8, #-762
mov r9, #-543
stmfa r13, {r5,r6,r7,r8,r9}

mov r5, #54
mov r6, #34
mov r7, #98
mov r8, #76
mov r9, #84
stmfa r13, {r5,r6,r7,r8,r9}

bl	max       	  ; place pc+4 into lr and jump to the function max
end                       ; stops the simulator

Exam question example

Explainations for the mnemonics used in the assembly snippet below:

 mov r8, r7       -- Copy value from register r7 to register r8
 add r8, r7, r6   -- Add value of r7 to r6 and store the result in r8
 sub r8, r7, r6   -- Subtract value of r6 from r7 and store the result in r8
 b label          -- *Always* jump to label
 cmp r8, r7       -- Compare r8 and r7 and set flags accordingly
 blt label        -- Jump to label *only* if the leftmost operand is smaller than the rightmost in previously executed cmp 

Follow the instructions and elaborate what are the values left in the registers after the instruction finishes executing.

 magic:
   mov r2, r0             -- Instruction address 0xff00
   mov r3, #0             -- Instruction address 0xff04
 loop:
   cmp r2, r1             -- Instruction address 0xff08
   blt finish             -- Instruction address 0xff10
   add r3, r3, #1         -- Instruction address 0xff14
   sub r2, r2, r1         -- Instruction address 0xff18
   b loop                 -- Instruction address 0xff1c
 finish:
   mov r0, r2             -- Instruction address 0xff20
   bx lr                  -- Instruction address 0xff24

Fill in the table:

Registers
r0 r1 r2 r3 ... r13 r14 r15
Instruction a1 a2 a3 a4 ... sp lr pc Top of the stack What happened?
bl magic 39 17 ? ? ... 0x8f00 0xfa00 0xff00 ? Function was invoked
mov r2,r0 ...
mov r3,#0 ...
...
...
...
...
...
...
...
...
...
...
...
...

What does this function do?

Assignment

  1. Given the function for finding the largest value in an array use ARM cross compiler as shown above to generate the assembly corresponding to the C source code (3p).
  2. Remove the compiler hints (mostly lines starting with dot), this should leave you less than 20 instructions. If it's more than that try different optimization flags. Analyze and comment the assembly as shown above (3p).
  3. Use your day of birth, month of birth, current hour, minute, second as arguments as array values and follow the instructions, what values are left in the registers and top of the stack after each instruction finishes? Use the table from here as a template (9p)
  4. Optional/extra: Elaborate step by step what happens in the processor: what instruction is being executed and what values are left in registers after instruction finishes? (3p)
  5. Optional/extra: Recompile with different optimization levels: -Os, -O0, -O1, -O2 and -O3. What differences did you notice? (3p)
  6. Optional/extra: Rewrite the function in assembly with NEON instructions. How much speed boost did you gain? (+10p)
  7. Optional/extra: What is the space complexity of the algorithm? What is the time complexity of the algorithm? Explain! (+4p)


libmax.c the library portion of the program, or simply put the function definition:

 int max(int *m, int count) {
      int largest = -99999;
      int i;
      for (i = 0; i < count; i++) {
          if (m[i] > largest) {
              largest = m[i];
          }
      }
      return largest;
 }

findmax.c, the invoking code:

 int main() {
     int nums[5] = {1,-3,2,0,5};
     printf("largest number is: %d", max(nums, 5));
 }

Assignment: Debugging ALU design (5p)

Bob had to ship the ALU design to the hardware manufacturer by yesterday. The project manager is angry about missed deadline. Bob still hasn't figured out why the ALU is not working properly. Help Bob by figuring out what's the problem.

If you're on Ubuntu 14.04 install GHDL and GtkWave:

sudo add-apt-repository ppa:pgavin/ghdl
sudo apt-get update
sudo apt-get install ghdl gtkwave git

If you're on Ubuntu 16.04:

wget https://launchpad.net/~pgavin/+archive/ubuntu/ghdl/+build/6057094/+files/ghdl_0.31-1pgavin2~trusty2_amd64.deb
wget http://cz.archive.ubuntu.com/ubuntu/pool/universe/g/gnat-4.8/libgnat-4.8_4.8.2-8ubuntu3_amd64.deb
wget http://cz.archive.ubuntu.com/ubuntu/pool/universe/g/gnat-4.8/gnat-4.8-base_4.8.2-8ubuntu3_amd64.deb
sudo dpkg -i libgnat-4.8_4.8.2-8ubuntu3_amd64.deb  gnat-4.8-base_4.8.2-8ubuntu3_amd64.deb  ghdl_0.31-1pgavin2~trusty2_amd64.deb 
sudo apt -f install
sudo apt install gtkwave

Clone the repository

git clone https://github.com/laurivosandi/vhdl-exercise

Use make to compile the components and run the testbench:

cd path/to/vhdl-exercise
make

Expected output of the testbench:

alu_testbench.vhd:77:9:@360ns:(report note): Finished testing addition operation of ALU
alu_testbench.vhd:106:9:@1us:(report note): Finished testing subtraction operation of ALU
alu_testbench.vhd:135:9:@1640ns:(report note): Finished testing NAND operation of ALU
alu_testbench.vhd:166:9:@2280ns:(report note): Finished testing NOR operation of ALU

However there are few bugs in alu.vhd, find the bugs and correct them. If this is your first experience with VHDL, take a look here.

Assignment steps

  • Fork the repo on GitHub, clone your repo, fix mistakes, commit and push changes, send link to your GitHub repo where you have fixed the issues (5p)
  • Optional/extra: Expand ALU to 8 bits (2p)
  • Optional/extra: Add another opcode bit and implement multiplication so opcode(2) high means we're doing multiplication, opcode(1) in case of multiplication selects between higher and lower bits of the result and opcode(0) is used to negate the second operand of multiplication. (+5p)

Assignment: Adders with NAND gates (5p)

In this assignment we use chips to build an adder. Get max points (4p) by wiring up half adder and double your points (8p) if you can make full adder work as well :)


Each CD74HC00E chip contains four NAND gates arranged like this, observe the pit on the left. VCC corresponds to 5V power supply and GND corresponds to ground or 0V.


Half adder

Each CD74HC00E contains 4 CMOS NAND gates. Use breadboard to wire up half adder with 7400 chips (4p):

The truth table for half adder:

  A  |  B  | Cout | Sum  | Human readable (how many bits are high?)
-----+-----+------+------+-------------------------------------------
  0  |  0  |  0   |  0   | 0
-----+-----+------+------+-------------------------------------------
  0  |  1  |  0   |  1   | 1
-----+-----+------+------+-------------------------------------------
  1  |  0  |  0   |  1   | 1
-----+-----+------+------+-------------------------------------------
  1  |  1  |  1   |  0   | 2

Full adder

You can double your points if you manage to wire up a full adder (+5p)

On a breadboard it looks a bit messy, but if you pay attention to gate numbering as shown above it should make your life significantly easier. The gates 1, 2, 3, 4 are placed in the leftmost chip; 5, 6, 7, 8 are placed in the middle one and gate 9 is the only one in the rightmost chip:



  A  |  B  | Cin | Cout | Sum  | Human readable (how many bits are high?)
-----+-----+-----+------+------+-------------------------------------------
  0  |  0  |  0  |  0   |  0   | 0
-----+-----+-----+------+------+-------------------------------------------
  0  |  0  |  1  |  0   |  1   | 1
-----+-----+-----+------+------+-------------------------------------------
  0  |  1  |  0  |  0   |  1   | 1
-----+-----+-----+------+------+-------------------------------------------
  0  |  1  |  1  |  1   |  0   | 2
-----+-----+-----+------+------+-------------------------------------------
  1  |  0  |  0  |  0   |  1   | 1
-----+-----+-----+------+------+-------------------------------------------
  1  |  0  |  1  |  1   |  0   | 2
-----+-----+-----+------+------+-------------------------------------------
  1  |  1  |  0  |  1   |  0   | 2
-----+-----+-----+------+------+-------------------------------------------
  1  |  1  |  1  |  1   |  1   | 3

Unittest

Use following code snippet on Arduino to test the circuit, it will shuffle through all the possible inputs and test whether the expected output is on the output pins. If you see sum or cout outputs giving 0 all the time it's likely you have resistors of too low resistance in the circuit which is draining all the voltage on the NAND gate output pins.

int PIN_A = 2;
int PIN_B = 3;
int PIN_CIN = 4;
int PIN_SUM = 5;
int PIN_COUT = 6;

void setup() {
    // Set up serial
    Serial.begin(9600);
    
    // Set up operands
    pinMode(PIN_A, OUTPUT);
    pinMode(PIN_B, OUTPUT);
    pinMode(PIN_CIN, OUTPUT);
    digitalWrite(PIN_A, HIGH);
    digitalWrite(PIN_B, HIGH);
    digitalWrite(PIN_CIN, HIGH);
    
    // Set up measurement pins
    pinMode(PIN_SUM, INPUT_PULLUP);
    pinMode(PIN_COUT, INPUT_PULLUP);
}

void test(int a, int b, int cin) {
    int cout = a && b || a && cin || b && cin;
    int sum = a ^ b ^ cin;
    
    digitalWrite(PIN_A, a);
    digitalWrite(PIN_B, b);
    digitalWrite(PIN_CIN, cin);
    
    Serial.print("Writing: a=");
    Serial.print(a);
    Serial.print(", b=");
    Serial.print(b);
    Serial.print(", cin=");
    Serial.print(cin);
    Serial.print("  Expecting: sum=");
    Serial.print(sum);
    Serial.print(", cout=");
    Serial.print(cout);
    
    int measured_sum = digitalRead(PIN_SUM);
    int measured_cout = digitalRead(PIN_COUT);

    Serial.print("  Got: sum=");
    Serial.print(measured_sum);
    Serial.print(" cout=");
    Serial.print(measured_cout);
    
    Serial.print("  Test:");
    if (measured_sum != sum || measured_cout != cout) {
        Serial.print("  FAIL");
    } else {
        Serial.print("  GOOD");
    }
    Serial.println();
    delay(500);
}

void loop() {
    Serial.println("Running test");
    test(0, 0, 0);
    test(0, 0, 1);
    test(0, 1, 0);
    test(0, 1, 1);
    test(1, 0, 0);
    test(1, 0, 1);
    test(1, 1, 0);
    test(1, 1, 1);
}

Deprecated assignment: Investigating microcontrollers

In the IoT assignment we already played around with ESP32 microcontroller, most microcontroller concepts are the same for Arduino (interrupts, PWM, etc).

In this assignment we use Arduino to implement traffic lights for crossroads. If you don't care much about doing this assignment hands on, you can give a try online.

Assignment steps

  • Install Arduino IDE on your personal machine, on Windows/Mac check out Arduino homepage for instructions, on Ubuntu apt install arduino should suffice. You can also use school computers, Ubuntu has Arduino preinstalled.
  • If you happen to have Java 9 running by default, check with java -version then you might have to switch to Java 8 instead: sudo apt install openjdk-8-jdk; sudo update-java-alternatives -s java-1.8.0-openjdk-amd64
  • From Arduino IDE menu open up File -> Examples -> 01. Basic -> Blink
  • In Arduino IDE menu Tools -> Board -> Arduino Mega2560
  • In Arduino IDE menu Tools -> Serial port -> Select what makes sense
  • On Ubuntu you may have to add yourself to the dialout group before you can access the serial ports. Use the command sudo gpasswd -a $USER dialout, log out and log in again to your PC.
  • Press the second button in the toolbar to upload the code, make sure the changes take effect on the board. You should see on-board LED blinking.
  • Connect some LED-s to the board. MAKE SURE YOU USE RESISTORS TO PROTECT LED-s AND THE BOARD
  • Implement traffic light cycle using digitalWrite and delay in the loop function (3p)
  • Use interrupts to read button presses, see example below. Pressing the button should toggle the LED now (1p)
  • Extra: Use PWM to implement smoother blinking (1p)
  • Extra: Use LCD1602 shield to display countdown (1p)
  • Use serial interface to send messages to your PC.
  • You can find useful code snippets for Robotics club equipment here

Code example: reading button press with interrupts

Set up a push button as shown below:

Insert following code to Arduino IDE and upload it to Arduino, verify that it works:

int led = 13;
int button  = 2;
int flag = LOW;

void setup() {
    pinMode(led, OUTPUT);         // Set up pin 13 as digital output
    pinMode(button, INPUT_PULLUP);       // Set up pin 9 as digital input with pull-up resistor enabled

    // Associate interrupt handler with an event
    attachInterrupt(digitalPinToInterrupt(button), onButtonPressed, FALLING);
}

void onButtonPressed() {
    // Keep this as short as possible
    flag = HIGH;
}

void loop() {
    // Do something useful or let processor sleep here
    if (flag) {
        digitalWrite(led, HIGH);
        delay(2000);
        flag = LOW;
    }

    digitalWrite(led, LOW);
    delay(50);
}

Code example: Cycling through different traffic light states

Add LED-s to the design:

Use following as hint to implement cycling through traffic light states:

// Define pin numbers
int car_green = 5;
int car_yellow = 6;
int car_red = 7;

// For LED-s we're sinking the 5V voltage, hence LED is turned on when voltage on the pin is LOW
int on = LOW;
int off = HIGH;

void setup() {
    pinMode(car_green, OUTPUT);
    pinMode(car_yellow, OUTPUT);
    pinMode(car_red, OUTPUT);
    digitalWrite(cars_yellow, off);
    Serial.begin(9600);
}

void loop() {
    Serial.println("Cars green, pedestrians red for 5 seconds");
    digitalWrite(cars_red, off);
    digitalWrite(cars_green, on);
    delay(5000);

    Serial.println("Green for cars is blinking 5 seconds");
    for(int j = 0; j < 10; j++) {
        digitalWrite(cars_green, j % 2 == 0);
        delay(500);
    }

    Serial.println("Cars red, pedestrians green for 5 seconds");
    digitalWrite(cars_green, off);
    digitalWrite(cars_red, on);
    delay(5000);
}


Skeleton of the final submission

The real traffic light has of course a little bit more states:

1. Cars green, pedestrians red for 10 seconds
2. Cars green **blinking**, pedestrians red for 2 seconds
3. Cars red, pedestrians green for 10 seconds
4. Cars red, pedestrians green **blinking** for 2 seconds
5. Cars yellow, pedestrians red for 2 seconds

So your final code will look something like this:

// Define pin numbers here

// Initially the flag is low
int pedestrian_requested_crossing = LOW;
 
void setup() {
    // Initialize necessary pins as digital outputs
    // Reset pins with digitalWrite

    // Initialize button pin as digital input
    // Enable pull-up resistor on button pin
    // Associate button interrupt handler function (onButtonPressed) below with the event on the button

}

void onButtonPressed() {
    // Here set pedestrian_requested_crossing flag to HIGH
    // Don't do anything else here!
}

void loop() {
    Serial.println("Cars green, pedestrians red for 5 seconds");
    digitalWrite(cars_red, off);
    digitalWrite(cars_green, on);
    // Set pedestrian lights here
    delay(5000);

    if (pedestrian_requested_crossing) {

        Serial.println("Green for cars is blinking 5 seconds");
        for(int j = 0; j < 10; j++) {
            digitalWrite(cars_green, j % 2 == 0);
            delay(500);
        }

        Serial.println("Cars red, pedestrians green for 5 seconds");
        digitalWrite(cars_green, off);
        digitalWrite(cars_red, on);
        // Set pedestrian lights here
        delay(5000);

        // Add another state here

        // And another state here

        // Reset flag here
    }
}

Deprecated assignment: Publishing your work

In the Git assignment most of the following was already required

  • Clean up your Git repositories and send the URL of a repository that you've actively worked on (for example the one from Java course) to Lauri, but before check the following:
    • The repository should show changes you've made over at least few days.
    • The repository has to contain only plaintext files unless there is a really good reason not to (eg. images).
    • There has to be authorship information in the files.
    • Add .gitignore file to your GitHub repositories to ignore temporary files, see Git docs for more details.
    • Add LICENSE file in the repository directory and commit the changes. LICENSE should answer to some important questions
      • Who is the copyright holder?
      • Under which conditions is redistribution permitted?
    • Add nicely formatted README file, see Markdown (online editor here) and reStructuredText for more details. README should answer to several silly questions:
      • What is the repository about?
      • Who made it?
      • How can the author be contacted? E-mail address, IRC chat channel on Freenode, Skype username?
      • How can the code be used? What hardware is necessary? How should the user wire the circuit to make it work? Add images if necessary.
      • What is the policy for including changes from third party developers?
  • There will be more details here

Extra points

Here you can claim extra credit points for various tasks, this is mostly to improve the quality of wiki.itcollege.ee. Once you have taken care of the task insert your name in the end of the line with corresponding date.

This category currently contains no pages or media.