https://github.com/freedosproject/toycpu Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code + Explore + All features + Documentation + GitHub Skills + Blog * Solutions + By Plan + Enterprise + Teams + Compare all + By Solution + CI/CD & Automation + DevOps + DevSecOps + Case Studies + Customer Stories + Resources * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles + Repositories + Topics + Trending + Collections * Pricing [ ] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} freedosproject / toycpu Public * Notifications * Fork 1 * Star 35 A simulation of a Minimal instruction set computer License MIT license 35 stars 1 fork Star Notifications * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Security * Insights freedosproject/toycpu This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 1 branch 3 tags Code * Clone HTTPS GitHub CLI [https://github.com/f] Use Git or checkout with SVN using the web URL. [gh repo clone freedo] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @freedosproject freedosproject Update LICENSE ... a6f7040 Oct 30, 2022 Update LICENSE Add email address to license file a6f7040 Git stats * 32 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time LICENSE Update LICENSE Oct 30, 2022 README.md Major edits to documentation Oct 30, 2022 binary.c Add files via upload Oct 30, 2022 binary.h Add files via upload Oct 30, 2022 build.bat Add files via upload Oct 30, 2022 clean.bat Add files via upload Oct 30, 2022 cpu.c New CPU handling Oct 30, 2022 cpu.h New CPU handling Oct 30, 2022 input.c New input using DOS graphical mode Oct 30, 2022 input.h New input using DOS graphical mode Oct 30, 2022 opcodes.h New opcodes.h Oct 30, 2022 screen.c Screen updates in DOS graphical mode Oct 30, 2022 screen.h Screen updates in DOS graphical mode Oct 30, 2022 toy.c Complete rewrite from scratch of toy.c Oct 30, 2022 View code [ ] Toy CPU Status License History How it works Programming Sample programs Flash the lights Flash the lights (alternate method) Move a light Countdown A + B = C Compare A and B README.md Toy CPU A simulation of a Minimal Instruction Set Computer Status Working version (FreeDOS graphical mode) License MIT History I teach university courses part-time, and one course that I teach is MIS 100, where students learn how technology works. For our section on "programming," I usually talk about programming in very abstract terms, because these are not Computer Science students. But this year, I wanted to try something new. I hoped to start the "programming" discussion by walking my students through a history of programming, so they could see the next step and how that worked. I tried to find a simple hobby "educational" computer, similar to the Altair 8800, where you input a series of program instructions in "machine language" (binary opcodes) using switches and LEDs. I wanted the instructions to be simple enough that my students could write a few simple programs, like A+B=C, and use that as a starting point to write the same program in Assembly, and in C, and ... you get the idea. But I couldn't find a suitable "Altair-like" SBC for less than $100. There are "Altair" software emulators out there, but they faithfully reproduce the Altair 8800, and that was too much overhead for my needs. So I decided to write my own hobby "educational" computer. I call it the Toy CPU. I wrote a rough prototype on DOS using character mode, but you couldn't enter a program on the "front panel." Instead, you had to hard-code a program into memory, and recompile to run that. It was very primitive, but enough to know that it worked. Later, I completely rewrote the Toy CPU using Linux ncurses. This was still a prototype, but this time you could enter a program on the "front panel" by "flipping bits." It looked okay in ncurses, but I really wanted a graphical program. Open Jam 2022 came up at the right time! I decided to completely rewrite the Toy from scratch, using FreeDOS and other open source tools. I don't use graphical assets per se; instead, the Toy draws the interface elements using basic graphical functions from OpenWatcom (open source). If you're curious: I used FED (open source) as my editor. I ran FreeDOS inside VirtualBox (open source) running on top of Fedora Linux (open source). Everything in the pipeline was open source. The theme for Open Jam 2022 is "Light in the Darkness," which is a perfect fit for the Toy CPU, because of all the blinkenlights! How it works I intentionally kept this as a very simple implementation. My goals were to make it easy to write and easy to understand. The Toy CPU implements 256 bytes of program memory, and an accumulator. You program the Toy using binary opcodes. When you run a program, the Toy CPU starts at counter zero for the first instruction. Programming When you start the Toy, it will "boot up" by clearing all the values in the 256-byte memory. You'll see it count up from 0 to 255 in the counter display, while the instruction and accumulator displays remain at zero. Look at the status display on the bottom-right of the Toy. You will see "PWR" when the Toy is turned on, and "INI" when the Toy is initializing. After initialization, you'll be put into Input mode. Look on the status display and you will see "INP" light up to indicate you are in Input mode. In Input mode, you enter a program using "switches and lights" on the front panel, similar to computers in the 1960s or 1970s. Use the Up/ Down arrow keys to select the instruction you want to edit, then press Enter to edit an instruction. Look on the status display, and you will see "EDT" light up to show you are in Edit mode. In Edit mode, use the Left/Right arrow keys to select a bit in the instruction, and hit Space to flip a specific bit in an instruction. When you're done entering an intruction, press Enter to go back to Input mode. From Input mode, press R to run the program. This always runs the program from the first instruction. The status display will light up "RUN" when the Toy runs your program. If you need to abort your program, press the Esc key during program execution and the Toy will light up the "ABT" in the status display before it drops you back into Input mode. To exit the Toy, press Q while in Input mode. The Toy will light up the "HLT" light on the status and then will quit to DOS. Here are the opcodes for the Toy CPU, as currently implemented: opcode Name Description 00000000 STOP Stop program execution. Bit shift the accumulator to the right by 1 position. 00000001 RIGHT The value 00000010 becomes 00000001, and the value 00000001 becomes 00000000. Bit shift the accumulator to the left by 1 position. 00000010 LEFT The value 00000010 becomes 00000100, and 10000000 becomes 00000000. 00001111 NOT Binary NOT the accumulator. The value 10101111 becomes 01010000. 00010001 AND Binary AND the accumulator with the value stored at an address. 00010010 OR Binary OR the accumulator with the value stored at an address. 00010011 XOR Binary XOR the accumulator with the value stored at an address. 00010100 LOAD Load the accumulator with the value stored at an address. 00010101 STORE Copy the accumulator into an address. 00010110 ADD Add the value stored at an address to the accumulator. 00010111 SUB Subtract the value stored at an address from the accumulator. 00011000 GOTO Jump to a program address. 00011001 IFZERO If the accumulator is zero, jump to a program address. 10000000 NOP No operation; safely ignored. Any other unrecognized instruction is the equivalent of a NOP. However, do not rely on other opcodes being the same as NOP. For example, in the current version of the Toy, any instruction with ...1.... will also perform an extra fetch operation. Sample programs Flash the lights It helps to understand the Toy CPU by looking at a sample program. In this example, we'll "flash" the accumulator lights. First we'll light up the lower bits, then the higher bits, then all 8 bits. This tests sequential operation of the Toy: 0. LOAD 1. "A" (7) 2. LOAD 3. "B" (8) 4. LOAD 5. "C" (9) 6. STOP 7. "A" = 00001111 8. "B" = 11110000 9. "C" = 11111111 This would be entered into the Toy like this: (counter: 00000000) instruction: 00010100 (counter: 00000001) instruction: 00000111 (counter: 00000010) instruction: 00010100 (counter: 00000011) instruction: 00001000 (counter: 00000100) instruction: 00010100 (counter: 00000101) instruction: 00001001 (counter: 00000110) instruction: 00000000 (counter: 00000111) instruction: 00001111 (counter: 00001000) instruction: 11110000 (counter: 00001001) instruction: 11111111 This is a series of LOAD instructions that first loads the value 00001111 into the acccumulator, effectively lighting up the right side of the accumulator. Then it loads the value 11110000, lighting up the left side of the accumulator. Finally, it loads the value 11111111, lighting up all the lights on the accumulator. Flash the lights (alternate method) A more efficient way to write this program is to use logical operators to operate on the initial 00001111 value. This loads 00001111 into the accumulator, then performs a logical NOT, resulting in 11110000. Then it uses OR with the original 00001111 value to get 11111111 before performing another NOT to give the final 00000000 result: 0. LOAD 1. "A" (7) 2. NOT 3. OR 4. "A" (7) 5. NOP 6. STOP 7. "A" = 00001111 If you want to edit a program that's already entered into memory, you may need to use NOP statements to skip over now-unused instructions. In this example, I modified the previous "Flash the lights" program, which had STOP at instruction 6. Since the updated program ends at instruction 4, I added a NOP statement at instruction 5 so the program would flow to the STOP at instruction 6. Move a light Or consider this sample program that moves a single light from the left to the right: 0. LOAD 1. "A" (8) 2. RIGHT ("loop start") 3. IFZERO 4. "end" (7) 5. GOTO 6. "loop start" (2) 7. STOP ("end") 8. "A" = 10000000 To make this program easier to read, I've added notes in parentheses for the "loop start" at instruction 2, and "end" at instuction 7. I always write out my programs on paper first, and using this notation makes it easier to go back later and fill in the instruction values. For example, use the value 00000111 (7) at instruction 4, and the value 00000010 (2) at instruction 6. Countdown Or this sample program that counts down from 15: 0. LOAD 1. "A" (9) 2. SUB ("loop start") 3. "one" (10) 4. IFZERO 5. "end" (8) 6. GOTO 7. "loop start" (2) 8. STOP ("end") 9. "A" = 00001111 (15) 10. "one" = 00000001 (1) A + B = C Consider this short program that adds two values, "A" and "B," and stores the result in the variable "C." 0. LOAD 1. "A" (7) 2. ADD 3. "B" (8) 4. STORE 5. "C" (9) 6. STOP 7. "A" = some number 8. "B" = some other number 9. "C" = placeholder If you enter and run this program, you should see instruction 9 stores the result of adding "A" and "B" together. It will be the same value as the accumulator when the program has stopped running. Note that whatever value was stored in "C" beforehand will be overwritten. Compare A and B Or this sample program that compares two numbers, "A" and "B." If they are the same, the accumulator displays 00000000. If they are different, the accumulator displays 11111111: 0. LOAD 1. "A" (9) 2. XOR 3. "B" (10) 4. IFZERO 5. "end" (8) 6. LOAD 7. "X" (11) 8. STOP ("end") 9. "A" = some number 10. "B" = some other number 11. "X" = 11111111 About A simulation of a Minimal instruction set computer Resources Readme License MIT license Stars 35 stars Watchers 1 watching Forks 1 fork Releases 3 Version 3.0 Latest Oct 30, 2022 + 2 releases Packages 0 No packages published Languages * C 99.5% * Batchfile 0.5% Footer (c) 2022 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.