r/AskComputerScience • • Dec 08 '22

I am working on an 8-bit computer using logic gates. This is the Instruction ASet Architecture I'm developing for it. 32 instructions, with a 5-bit opcode for each. I'm having trouble coming up with ideas for the extra one [01110]. Might I get some help in this sub?

Post image
31 Upvotes

25 comments sorted by

8

u/not_from_this_world Dec 08 '22 edited Dec 08 '22

Might as well leave it alone. Isn't more OP making it harder to build anyways? You have plenty of room to step up the challenge, virtual memory, parallelization, more bits, etc... You may save instruction space to those instead of rebuilding eveything.

I can see you already stretched out with a bunch of NOT(OP) but didn't brother making a simple NOT, I know you will use NAND for the NOT but if you need to have one more OP then might add the NOT as well.

Also an unrelated tip: in literature of load–store architecture the processor loads from memory and stores to memory, your circuit is working the other way around.

2

u/AnimusFoxx Dec 08 '22 edited Dec 08 '22

The thing about building these things in Minecraft is that everything takes up a lot of space, and redstone (the block/system used for building circuits) does not work the same way as electricity. For one thing, signals can generally only be carried one-way. For another, it's not instant, and every small circuit adds a lot of delay to your signals.

With everything being uni-directional and taking up a lot of space, it can be very hard to add circuits to a build like this once it's completed, there likely just won't be room. Also, moving components around is no trivial matter, as their placement dictates the structure of everything around them, and how quickly everything can communicate. So that's why I'm trying to have the ISA completed before I construct the control unit and lock in the structure of the data loop. "Adding more bits" is completely out of the question for this CPU, because it would mean entirely overhauling the design and starting from scratch. In a future design I may attempt that, but not this one

I'm also trying to avoid redundancy. That's why I don't have a "reg A = reg B" instruction; Predicate already does this, and you can just set the condition to unconditional. So like you said, I was planning on using NAND for NOT

2

u/not_from_this_world Dec 09 '22

I'm also trying to avoid redundancy.

In regard to this, I have no idea what you mean. You already have plenty of redundancy. All jumps plus the predicate could be replaced by one unconditional and one conditional jump. You have the bitwises for comparing, why do you need conditional instructions? Also, half the bitwise, all the increments and maybe even the subtract could be replaced by a single INC and a NOT. The input/output usually are part of the load/store IRL. I'm not saying you're wrong or anything, you do you. I'm saying I have no idea what you mean by "avoid redundancy".

Idk how things are done in minecraft my assumption is that the shorter the sinal path the faster it executes. Also there is a lot of circuitry implied in many of your OPs, you'll have a "condition evaluation" right? I'm just going for the principle that the less instructions you have the smaller the control unit, the smaller and shorter the data path, the smaller the circuit, the easier to build, right?

But like I said it's your project, your call, I'm just extending my previous answer that sums up as sometimes less is more.

If you're still going for a more CISC oriented approach and still need an extra OP then my new suggestion is MOV, moving data from the memory to the memory without a software loop of load/stores. Very important and often used OP. A reg for source address, another for dest address and a third reg for size. But it is a large circuit, your call.

2

u/AnimusFoxx Dec 09 '22 edited Dec 09 '22

Forgive me, I'm still new to this. I'm not in school so this is all being done in my free time. I don't know anything about CISC. Condition evaluation is just a small circuit that checks the condition operand against the ALU flags which are stored in a small non-addressable register. How can you replace all of the bitwise ops with NOT and INC?

Predicate in this context is used to avoid jumps altogether because they cause a massive stall in the program while the new instructions are loaded from ROM since normally the next few instructions are already loaded into a cache and jumping necessarily clears that cache. So predicate is used when you would rather run both possible sets of code and then copy the preferred result into whichever register will be used by the program after that. Is this not what predicate is usually used for? This way predicate can also be used for move, because you can just use the "no condition" condition.

I'm not entirely sure how the conditional jumps can be condensed, because there are only enough bits in the instruction for the 5-bit opcode, 3-bit condition, and an 8-bit program counter address. If I had two more bits, I could eliminate the IF FALSE versions as well as specify whether it's supposed to use a pointer or an immediate address, but I don't know how to do it within the 16-bit instruction length.

3

u/Poddster Dec 09 '22

Also, half the bitwise, all the increments and maybe even the subtract could be replaced by a single INC and a NOT. How can you replace all of the bitwise ops with NOT and INC?

In 2's complement x-y is the same as x+(~y+1)

aka SUB X,Y is the same as

NOT Y
INC Y
ADD X,Y

NOR, NAND, NXOR :

  AND X, Y
  NOT X

  OR X, Y
  NOT X

  XOR X, Y
  NOT X

1

u/AnimusFoxx Dec 09 '22 edited Dec 09 '22

But that's 2-3 times as many clock cycles per operation. I'm already looking at maybe a 0.5hz clock. Also I'm not sure if I'm going to attempt a paged program cache, so the program length might be very limited, maybe only 256 instructions if I go with a 8-bit program counter address

2

u/Poddster Dec 09 '22

But that's 2-3 times as many clock cycles per operation.

Welcome to the hardware design! Trade-offs are everywhere.

The question is: How often do you programs do XNOR? :) Is there something else they do more frequently that you could have a specific instruction for?

2

u/not_from_this_world Dec 09 '22 edited Dec 09 '22

OK, I'm sorry I though you were a undergrad.

We have CISC and RISC instruction set architecture, the wiki can fill you with the details.

CISCs besides the simple instructions like "add 1" include instructions which act as mini-programs or functions or blocks of code. They usually require several clock cycles to complete and you have to care about aborting the execution of some instructions sometimes.

The whole point of RISC architecture (at least in the beginning) was to have very simple, very fast instructions so they can be executed in a single clock cycle. Also you have a very simple circuit in general. The trade off is that you have to use several small instruction to accomplish the same result of a CISC instruction BUT your circuit is now smaller and faster. This dude on youtube explain how a computer can have a single instruction and do everything a general processor does. I wouldn't go that far because remembering the trade-off your CPU would be simple but your software would be CRAZY complex.

If you're learning as you adventure into this I recommend you go for the RISC way first.

How can you replace all of the bitwise ops with NOT and INC?

Not all, I said half of them, basically removing the XOP version you can still have the same result by giving 2 instructions. Like OR A,B then NOT A.

Predicate in this context is used to avoid jumps altogether because they cause a massive stall in the program while the new instructions are loaded from ROM since normally the next few instructions are already loaded into a cache and jumping necessarily clears that cache.

You usually jump a lot in a program, so I don't really understand your approach here.

Is this not what predicate is usually used for?

I have no idea how would you "run both possible sets of code" on an instruction level so I have no idea what your predicate is doing I can't answer this.

I'm not entirely sure how the conditional jumps can be condensed

The unconditional jump is need for the general flow of the software. It's just OP code and the address relative or not. The conditional jump works like this: if register = 0 then jump only the next instruction. It might be jump only when register != 0 too, or 2 registers are equal, you got it. They key is you just need one, you adapt the software around it. Now you make all those conditions into main code and feed that register with the result, then jump.

Using JZ as jump when register equals zero and JMP as unconditional jump then IF, ELSE in C would look like:

... TEST ALL CONDITIONS, RESULT GOES INTO REGISTER A
11    JZ A   // jumps just the next instruction if register A==0
12    JMP 10 // jumps 10 instructions ahead
13    XXX    // code when A==0 begins here
....
21    JMP 8  // end of A== 0 code, jump the next part into the rest of the program
22    XXX    // code when A!=0 begins here
....
29    XXX    // rest of the program begins here

example of for(int i = 10; i > 0; i--):

        SET C, 10
loop: 
       ... do stuff without using C register
       DEC C
       JZ C
       JMP @loop

2

u/AnimusFoxx Dec 09 '22 edited Dec 09 '22

You usually jump a lot in a program, so I don’t really understand your approach here.

Well yeah the idea is to mitigate jumps, not eliminate them entirely.

I have no idea how would you “run both possible sets of code” on an instruction level so I have no idea what your predicate is doing I can’t answer this.

The idea at least as I understand it is like this;

11 A = random number
12 XXX //code that manipulates A
13 B = C
14 XXX //code that manipulates A
15 A = C
16 A = B if condition is true
17 XXX //code that uses A

It achieves the same end result as a conditional jump, without the need to change places in the program counter. I might be completely missing the function and purpose of "Predicate" though. I had a lot of trouble trying to figure it out even after googling and asking around in the Minecraft computer community for more hours than I'd like to admit. If I'm wrong could you help me understand how predicate is supposed to work?

1

u/not_from_this_world Dec 09 '22

I see.

You used the word in a different context so I didn't know what you were talking about, now you gave the context.

You have an instruction called "predicate" but predication is not an instruction. It's a part of every instruction. Like you save some bits in every single instruction for the predicate. Then very similar to your "condition" part you encode conditions in the predicate usually using a specific register, like "register X is 0". The predicate may be a single bit associated with a specific register. If that condition is true then the instruction is not executed.

The part of your code you show is usually handled using memory. In fact you're using registers B and C as memory here. You would 1) manipulate A, store A in memory address X. 2) Continuing manipulating A, store the new A in memory address Y. 3) Repeat or do something else. 4) Loads X and Y from memory into A and B then compares A=B.

1

u/WikiSummarizerBot Dec 09 '22

Complex instruction set computer

A complex instruction set computer (CISC ) is a computer architecture in which single instructions can execute several low-level operations (such as a load from memory, an arithmetic operation, and a memory store) or are capable of multi-step operations or addressing modes within single instructions. The term was retroactively coined in contrast to reduced instruction set computer (RISC) and has therefore become something of an umbrella term for everything that is not RISC, where the typical differentiating characteristic is that most RISC designs use uniform instruction length for almost all instructions, and employ strictly separate load and store instructions.

Reduced instruction set computer

In computer engineering, a reduced instruction set computer (RISC) is a computer designed to simplify the individual instructions given to the computer to accomplish tasks. Compared to the instructions given to a complex instruction set computer (CISC), a RISC computer might require more instructions (more code) in order to accomplish a task because the individual instructions are written in simpler code. The goal is to offset the need to process more instructions by increasing the speed of each instruction, in particular by implementing an instruction pipeline, which may be simpler given simpler instructions.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

5

u/ghjm MSCS, CS Pro (20+) Dec 09 '22

You might find it more convenient if some of these instructions were organized in a way that makes the decoder logic easier. For example, when loading or storing a value to RAM, you will need to assert an address and also a high or low on the R/W pin. If the load RAM and store RAM instructions were, say, 00010 and 00011, then you can do a four bit comparison to determine that this is a memory operation, then just drive the R/W pin directly from the last bit of the instruction.

You also seem to be missing a number of addressing modes. There's a "load pointer," which I take to be an indexed addressing mode, but there's no equivalent store operation.

And last but not least, a number of these instructions are redundant. You don't need increment and decrement operations if you have add immediate and subtract immediate; you don't need all those logical operators (if you have AND and NOT then you don't need a dedicated NAND, etc).

Note: I don't know anything about Minecraft. These suggestions are based on actual computers.

1

u/AnimusFoxx Dec 09 '22

Interesting suggestions. I see your point about eliminating INC and DEC, also youre right I am missing the store pointer instruction. It looks like there's a fair amount of condensing I can do. Much of this is new to me so I greatly appreciate it. I'm having trouble understanding how I can condense the bitwise operations. Somebody else said I only need NOT and INC and can discard the rest. How do I achieve all of the bitwise ops with only those?

2

u/ghjm MSCS, CS Pro (20+) Dec 09 '22

Most CPUs have AND and OR instructions, negation, and shift left / shift right instructions. You might take a look at some actual CPUs, like the 6502 and Z-80.

Also, it occurs to me that you don't have any stack instructions (PUSH/POP), subroutine return (RET), or interrupt operations (SWI/IRET).

1

u/AnimusFoxx Dec 09 '22

What do those instructions do?

2

u/ghjm MSCS, CS Pro (20+) Dec 09 '22

PUSH takes the value from a register, writes it to the location pointed to by the stack pointer (which is another register), and decrements or increments the stack pointer depending on which way you want the stack to grow. POP retrieves the current value from the top of the stack and increments or decrements the stack pointer.

You need a stack if you want to be able to call functions. You might do this with a CALL instruction like on x86, which writes the IP to the stack, decrements the stack pointer, and transfers control to the address parameter to the instruction. Then there's a RET instruction that pops the return address from the stack and jumps to it.

Interrupts are external conditions that cause the CPU to run a particular piece of code, called an interrupt handler. For example, you might get an interrupt when a character is ready for input from a serial device. Receiving an interrupt is like a CALL instruction - the CPU automatically pushes the current IP to the stack and jumps to the handler. Typically you don't want interrupts to interrupt interrupt handlers, so the CPU has a mask that blocks further interrupts until the handler returns. The IRET instruction is like RET but it also clears this mask.

SWI - software interrupt - allows calling an interrupt handler from code. This instruction is called INT on x86, and was used for system calls - INT 21 was a system call on MS-DOS, and INT 80 was a syscall on Linux.

Interrupt handlers also need PUSH and POP instructions, because they need to make use of registers to do anything, but any registers whose values they change must be restored before the IRET, to avoid breaking user code. As a result, an interrupt handler looks like several PUSH instructions, then some functionality (maybe an IN), then several POP instructions and an IRET.

2

u/AnimusFoxx Dec 08 '22

There's a typo in the title - ASet should be Set. I'm building this computer in Minecraft, and thus is my first CPU build ever, so the instruction can't be anything too fancy.

2

u/ReginaldDouchely Dec 08 '22

Well, since it's in minecraft, you've got the opportunity to implement halt and catch fire, but with real in-game fire

1

u/tilrman Dec 09 '22

I think you may have your "load" and "store" terms swapped in some places.

You have an instruction to copy the contents of a register to RAM at the address stored in another register. You could add the complementary instruction to copy a value from RAM at the location of one register into another register. (Or maybe you lack the former and have the latter.)

1

u/AnimusFoxx Dec 09 '22

That's an idea! I may use this. It seems obvious now that you say it, but hey, I'm a bit bone headed sometimes.

And yeah I do have those terms swapped by accident. Thanks for pointing that out

1

u/Sir_B Dec 09 '22

A bitwise shift left might be useful.

1

u/AnimusFoxx Dec 09 '22

You can shift right by 7 bits, which is the same as 1 to the left

1

u/Poddster Dec 09 '22

Make that instruction the EXTEND instruction. If present it means you go into a second byte's worth of instruction. Chain them as necessary. (Though traditionally you'd have it as 0b11111 or something).

Or perhaps a modifier that turns any instruction using registers into one using immediate? That way you can get rid of all of the duplicate instructions.

1

u/Ventigon Jan 19 '23

I earlier found a nice book called "But How Do It Know" and it made me loving that kind of thing. It is so cool when you know what this guy is talking about. Maybe I could... Get some advantages from this post as I have no ideas what to add in a computer haha Nice work, looks cool