How to Create Your *OWN* Custom CPU in Sketchware! (Part 1)

Icon

Disclaimer: Explaining on this blog is simplified to make better understanding.

Hewwo all!👋 I'm Iyxan23, and today I will guide you on how to create your OWN custom CPU in Sketchware using Java codes or you can say "Add Source Directly" codes.

So, What the heck is a CPU? 🤔

Ah yes, If you don't know what a CPU is, that's not a problem because I will explain it in a few seconds.

So, In a nutshell, CPU is a kind of hardware that does everything your computer does, For example: Doing arithmetic stuff, Render your entire display, Detect your keyboard press, Detect mouse movement and a heck lot more.

CPU is the most important hardware for your Computer, But, Can it be created using software? Yes, you can, but not as good as a physical CPU. 

In this blog, we are creating a Super SIMPLE CPU using Java codes and shoved it into sketchware 😅.

Ok, Let's start! 😀


The first thing we do is make a table containing the instruction set we are adding to our own custom CPU. And yes, we are creating a simple 8-bit CPU.

Disclaimer: The instruction set can be anything, So it would not be able to run external programs.


But wait, What is an instruction set? 🤔

An instruction set is a set that provides commands to the processor, to tell it what it needs to do.
Imagine an instruction set is just a regular programming language function but represented by binary. 😃

Example Instruction set:
0000 - Halt
1000 - Set Register PARAM1 to be Next Byte and Jump 1 line.
0100 - Add Register PARAM1 and Register PARAM2 then store it to Register PARAM1.
1100 - Print Register PARAM1 into output.
and more...

Ok, Second we need to understand what a Register is.

We can say a register is a tiny storage that can store 1byte of data, Register is used to manipulating data/value or testing data without altering the actual value on the memory. Manipulated data usually stored back to the main memory.
In our CPU, We have 4 different Registers.
We can represent it using binary,
Which is 00, 01, 10, and 11 😅

Third, What is an instruction PARAM is

Remember, we are making an 8-Bit CPU, so the CPU can only execute 8Bits of data. 4 Of them are used to specify what instruction is it using, and 4 of them are used for PARAM/Parameter.

Imagine, the first 4 of the bits is a function name. Then the last 4 is used for the parameter of that function. That's how it works. 😅

Example: 
Example Instruction set: (only showing one)
...
1011 - Print PARAM1 as Integer into the screen
...

What the computer will do:
1011 0110
The instruction its calling is 1011, and the Parameter is 0110
So what the CPU will do is, Print the value 0110 (Which is 6) into the screen!


Another Example:
Another Example Instruction set: (only showing one)
...
0100 - Add Register PARAM1 and Register PARAM2 then store the value into Register PARAM1
...

What the computer will do: 
0100 00 01
The instruction its calling is 0100, and the Parameter is 00 and 01 (Because it uses PARAM1 and PARAM2)
What will the CPU do is, Get the value from register 00 and 01 then add them up, Then store the added value into register 00. Simple!

Ask anything you don't understand in the comments! I'm here to help :D

And will be continued at PART 2 on starting to make our own instruction set and initialize the app.

(Hope You know what I'm saying because my main language is not English :3)
Also, Thanks for reading my Blog! It really helps me a lot :D


Have a good day! 😁

4 comments:

Note: Only a member of this blog may post a comment.

Powered by Blogger.