{"id":1143,"date":"2025-11-20T20:28:06","date_gmt":"2025-11-20T12:28:06","guid":{"rendered":"https:\/\/vm1.go2see.me\/?p=1143"},"modified":"2025-11-20T20:28:06","modified_gmt":"2025-11-20T12:28:06","slug":"embedded-c-programmming-on-risc-v","status":"publish","type":"post","link":"https:\/\/vm1.go2see.me\/?p=1143","title":{"rendered":"Embedded C Programmming on RISC-V"},"content":{"rendered":"<h1>Embedded C Programmming on RISC-V<\/h1>\n<p>This lab is done <strong>INDIVIDUALLY<\/strong>.<\/p>\n<h2>Getting Started with Hardware<\/h2>\n<p>The following instructions are adapted from Lab 0 for use with C.<\/p>\n<p>Connect your FPGA board to your laptop and a VGA monitor.  There are VGA<br \/>\nmonitors in the lab with a BLUE connector on the end. If you do not have a VGA<br \/>\nmonitor at home, you can use CPUlator to get started (see below), but to you<br \/>\nwill be marked with things running on your FPGA board.<\/p>\n<p>In Windows, go to <strong>Start<\/strong> &#8211;&gt; <strong>Nios V Command Shell<\/strong> and run the following commands on your laptop:<\/p>\n<pre><code>make DE10-Lite # VGA will be blank\nmake GDB_SERVER<\/code><\/pre>\n<p>(alternatively, use <code>make DE1-SoC<\/code>)<\/p>\n<p>The first command transmits a Nios V computer system to your DE10-Lite board.<br \/>\nThe second command will start a program on your laptop that you need to leave running &#8212; it<br \/>\ncontinuously communicates between your Nios V computer system and your laptop.<br \/>\nIf you exit the GDB server by typing Q, the commands below will not longer work.<br \/>\nSometimes things get confused and you need to stop everything, including the GDB server,<br \/>\nand start over. Sometimes the GDB server can&#8217;t find the JTAG server to start &#8212; try starting Quartus<br \/>\nand then re-run the command.<\/p>\n<p>Again, go to <strong>Start<\/strong> &#8211;&gt; <strong>Nios V Command Shell<\/strong> and run the following commands on your laptop:<\/p>\n<pre><code>make TERMINAL<\/code><\/pre>\n<p>This launches a program to listen and display any characters sent by your Nios V computer over the JTAG UART.<br \/>\nLeave this program running to capture everything printed by your program. Control-C will exit.<\/p>\n<p>You can merge windows by dragging the tab for the Nios V Command Shell window over the tab from the first window.<\/p>\n<p>Again, go to <strong>Start<\/strong> &#8211;&gt; <strong>Nios V Command Shell<\/strong> and run the following commands on your laptop:<\/p>\n<pre><code>make COMPILE\nmake RUN<\/code><\/pre>\n<p>The first command compiles your program, <code>vga.c<\/code> into an object file,<br \/>\n<code>vga.c.o<\/code>, and then linking it into an executable, <code>vga.elf<\/code>.  The second<br \/>\ncommand transfers the <code>vga.elf<\/code> executable to your Nios V computer and runs it.<\/p>\n<p>This Nios V program will print the message &quot;start&quot; to the TERMINAL window you started earlier, then it will<br \/>\nfill the VGA screen with colour bar pattern, then it will print the message &quot;done&quot;.<\/p>\n<p>You can edit your vga.c program, save it, then run <code>make COMPILE<\/code> and <code>make RUN<\/code> again.<br \/>\nHint: the easiest way to edit is with <code>notepad vga.c<\/code>, but you can also right-click the<br \/>\nfile and use notepad++ or launch WSL and run vim.<\/p>\n<h3>Using GDB<\/h3>\n<p>After <code>make COMPILE<\/code>, you can also launch gdb to debug your program:<\/p>\n<pre><code>make GDB_CLIENT<\/code><\/pre>\n<p>Note: for some reason, GDB is not working with C code. Use CPUlator instead.<\/p>\n<h3>Using CPUlator<\/h3>\n<p>Instead of using GDB and using your FPGA board, you can run things on CPUlator.<br \/>\nThis tool, written by a former UBC Master&#8217;s student \/ UofT PhD student Henry Wong,<br \/>\nperfectly emulates the DE1-SoC Computer System with RISC-V as well as several<br \/>\nother computers and processors. Henry runs this website on his own.<\/p>\n<p><a href=\"http:\/\/cpulator.01xz.net\">http:\/\/cpulator.01xz.net<\/a><\/p>\n<p>To get started:<\/p>\n<ul>\n<li>select RISC-V RV32 under Architecture<\/li>\n<li>select RISC-V RV32 DE1-SoC under System<\/li>\n<li>select Go<\/li>\n<li>in the Editor window, change Language from RV32 to C<\/li>\n<\/ul>\n<p>All of your source code has to appear in the one edit window (as a single file).<br \/>\nFor this lab, first paste the contents of <code>vga.c<\/code> into the Editor window. Then,<br \/>\nfind the line that says <code>#include &quot;address_map_niosv.h<\/code> and delete it; in its place,<br \/>\npaste the entire contents of the file <code>address_map_niosv.h<\/code>.<\/p>\n<p><strong>WARNING<\/strong> since CPUlator is a website, pressing REFRESH on the web page might<br \/>\ncause your program to disappear. Save if locally using the <code>File --&gt; Save ...<\/code> feature.<br \/>\nLikewise, you can load it back again using <code>File --&gt; Load...<\/code>.<\/p>\n<h2>Lab 9 Requirements<\/h2>\n<h3>Tron Light Cycle Game<\/h3>\n<p>Modify vga.c into a C program that plays the Tron light cycle on your FPGA<br \/>\nboard.  Requirements for the game are described below.<\/p>\n<p>The game is played with 2 players. One player must be a human, the other must<br \/>\nbe a robot (described below).  You must determine which controls to use for the<br \/>\nhuman. (Optionally, you may allow a second human to force the robot to make a<br \/>\nturn.)<\/p>\n<p>The game starts with an all-black screen. A white border should be drawn around<br \/>\nthe perimeter of the screen, forming the gameplay area. The game designer can<br \/>\nadd fixed obstacles (white). Obstacles may be single pixels, rectangles, lines,<br \/>\nor any other shape that you wish.  Collision with the border or an obstacle<br \/>\nresults in death.<\/p>\n<p>You can determine collision by reading the pixel colour from the VGA buffer at<br \/>\nthe desired location, eg <code>pixel_t p = *pVGA;<\/code> will read the pixel colour at<br \/>\nlocation [0,0] into the variable p.<\/p>\n<p>Each player controls a light cycle which starts at a fixed position roughly<br \/>\nhalfway down the screen and one-third of the way from the left and right edges.<br \/>\nThe light cycle is a single pixel that constantly moves across the screen and<br \/>\nleaves behind a coloured light trace. The player controls whether the light<br \/>\ncycle is moving up, down, left, or right, but it must always move by one pixel<br \/>\nafter every time delay. The light trace is an electrified wall that kills any<br \/>\nplayer upon collision (including yourself!).<\/p>\n<p>The winner is the first player to reach a score of 9. For each player, you must<br \/>\nalways display the current score (0-9) using one 7-segment display each. A player<br \/>\nscores one point by surviving longer than the other player; it is possible that both<br \/>\nplayers die at the same time, in which case neither player scores a point. After a<br \/>\ncomplete game (reaching 9), the screen must fill up with the light of the winning<br \/>\nplayer.<\/p>\n<p>For a simple robot player, have it look one and two pixels ahead. If it detects<br \/>\nanything other than an unclaimed pixel (still black), it first attempts to turn<br \/>\nLEFT. If the pixel to the LEFT is not black, then it attempts to turn RIGHT. If<br \/>\nit cannot turn LEFT or RIGHT, then it continues straight. You may modify this<br \/>\nbehavior to make your robot player better.<\/p>\n<p>This game is deliberately open-ended to allow you to either simplify or be<br \/>\ncreative as you see fit.<\/p>\n<p>It is recommended that you start with one working player (either human or robot),<br \/>\nthen add the second player, then add the score-keeping and display.<\/p>\n<h3>Switching between CPUlator (DE1-SoC) and DE10-Lite<\/h3>\n<p>If you have a DE1-SoC, or are running in CPUlator, modify the beginning of<br \/>\n<code>address_map_niosv.h<\/code> so it reads <code>#define DE10LITE 0<\/code>. If you switch from<br \/>\nCPUlator back to DE10-Lite hardware, be sure to change the 0 back to a 1.<\/p>\n<h3>Deadline<\/h3>\n<p>Due to the late release of this lab, you may get it marked at the same time as<br \/>\nLab 10. However, it is strongly advised that you attempt to get it done this<br \/>\nweek.<\/p>\n<h3>Submission Requirements<\/h3>\n<p>Submit your entire program, as a single C source file. However, please rename<br \/>\nyour source file to end in <code>.txt<\/code> so that Canvas can actually display its contents<br \/>\ninstead of forcing a file download.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Embedded C Programmming on RISC-V This lab is done INDI&#8230; &raquo; <a class=\"read-more-link\" href=\"https:\/\/vm1.go2see.me\/?p=1143\">\u95b1\u8b80\u5168\u6587<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1143","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts\/1143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1143"}],"version-history":[{"count":2,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts\/1143\/revisions"}],"predecessor-version":[{"id":1145,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts\/1143\/revisions\/1145"}],"wp:attachment":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}