

Simple 4-bit/1 Hz virtual computer for learning purposes - mirceasoaica
https://github.com/gto76/comp-cpp

======
daveloyall

                 RAM  
        ADDR  ________ 
        ---- |----***-|
        ---* |---*****|
        --*- |--*-*--*|
        --** |---****-|
        -*-- |-*------|
        -*-* |--------|
        -**- |--------|
        -*** |--------|
        *--- |--------|
        *--* |-------*|
        *-*- |--------|
        *-** |--------|
        **-- |--------|
        **-* |--------|
        ***- |--------|

~~~
ketralnis
You can do it in one fewer clock cycle per loop by just using the register
instead of the extra scratch memory space :) (I also reset the loop at the end
or it would be two fewer):

    
    
                 RAM  AD PC
        ADDR  ________ _ _
        ---- |---*****|-|*| ; OUTPUT the register
        ---* |-*-*-*--|-|-| ; IF MAX, goto LBL_RESET
        --*- |--*--**-|-|-| ; else add ONE to the register...
        --** |-*------|-|-| ; ...and start over.
        -*-- |-----***|-|-| ; LBL RESET: read in ZERO to the register...
        -*-* |-*------|-|-| ; ...and start over.
        -**- |-------*|-|-| ; ONE (r/o data)
        -*** |--------|-|-| ; ZERO (r/o data)

~~~
daveloyall
Oh! Yes, that's totally what registers are for, huh? :) Thank you.

------
userbinator
_Computer has one 8 bit register._

That would be enough to classify it as an 8-bit machine, despite the fact that
instructions are only 4 bits wide. A real 4-bit CPU, like the 4004, has 4-bit
registers.

~~~
tr352
I'd say, more importantly, it has an 8-bit data bus. It is the 4 bit address
bus which cripples it. By comparison, the 4004 has a 4 bit data bus and 4 bit
registers, but a 12 bit (4KByte) address bus.

~~~
userbinator
The Pentium has a 64-bit databus, yet its general-purpose registers are only
32 bits and it is classified as a 32-bit machine.

This gets even muddier with the Pentium MMX, which has a set of 64-bit MMX
registers. Of course there's also 80-bit-wide registers in the FPU of
everything back to the 8087...

Bitness is a surprisingly subtle thing to define, but I think an 8-bit-wide
GPR is enough for this one to be called 8-bit.

~~~
tr352
> The Pentium has a 64-bit databus

Didn't realize that, and you're right. Other examples of CPU's that I'm aware
of with a different "bitness"/databus width are the 68008 and 8808, but in
both cases the data bus is smaller than the "bitness".

~~~
cmrdporcupine
Sure, or the 68k itself which has a 16bit physical data bus but 32bit
registers, spawning 30+ years of pointless arguing over whether to classify it
as 32 bit or 16 bit.

------
ataylor284_
This reminds me of my Gakken GMC-4. It's a 4-bit computer like this, but a
physical piece of hardware. It has 7 LEDs, one 7-segment LED, a 20-key keypad
and a tone generator. You have to key in the programs manually on the keypad,
but you've only got about 100 4-bit words into which to fit your code and
data. It's further constrained by the fact you can only directly address 16 of
them for data. Even so, it's surprisingly fun and challenging to write little
assembly programs for.

------
ketralnis
I wrote a little fibonacci:

    
    
                 RAM  AD PC
        ADDR  ________ _ _
        ---- |---****-|-|*|
        ---* |----**-*|-|-|
        --*- |---***--|-|-|
        --** |--*-***-|-|-|
        -*-- |---***-*|-|-|
        -*-* |----**--|-|-|
        -**- |---*****|-|-|
        -*** |-*------|-|-|
        *--- |--------|-|-|
        *--* |--------|-|-|
        *-*- |--------|-|-|
        *-** |--------|-|-|
        **-- |--------|-|-|
        **-* |-------*|-|-|
        ***- |--------|-|-|
        **** | OUTPUT |-|-|
    

But due to the saturating arithmetic, it gets to 255 and then just prints 255
over and over. I tried to write one that would reset itself when that happens,
but I can't seem to do it in 15 bytes (I have a 14 instruction+3 variable
version but of course 17 bytes won't fit in RAM)

Edit: I can save one of the variables with a set-immediate instruction :)
[https://github.com/gto76/comp-cpp/pull/7](https://github.com/gto76/comp-
cpp/pull/7)

------
fbonetti
Can anyone give me a super dumbed-down explanation of how to operate this
machine?

If I understand the README correctly, it looks like the left 4 bits are the
instruction, and the right 4 bits are the address. So the line ---1--1- would
mean "READ the value from address 0010 into the register". Is the correct
interpretation? If so, how do I put a value onto the register in the first
place? How I do I set the value at an address?

Let's say I wanted to add 0001 + 0001 and show the output as 0010 on the
printer. How would I do that?

------
vadiml
I wonder why RAM_SIZE = ADDR_SIZE * ADDR_SIZE - 1? I mean why -1?

~~~
unwind
And why ADDR_SIZE squared? That just seems to work "by accident", since
ADDR_SIZE is 4 it evaluates to 15. What is really meant is probably (1 <<
ADDR_SIZE), i.e. 2 raised to ADDR_SIZE. Of course for the value 4 it evaluates
to the same thing. For 5 it would become 24 rather than 31 which I would
expect, assuming it's a size in bits of course ... Now I'm confused.

The file in question: [https://github.com/gto76/comp-
cpp/blob/e800d121e40f8acca6b9d...](https://github.com/gto76/comp-
cpp/blob/e800d121e40f8acca6b9d0c4f2fe299fce012eee/src/const.hpp).

And the -1 seems to be explained by how the author wants to use it, it's "last
valid address", i.e. _not_ the size at all. Something I'd complain about if
this were open for review, for sure. :)

------
circa
"my CPU is a neural net processor a learning computer"

I can't be the only one who thought this, right?!

~~~
nsxwolf
Humor is not allowed on HN, but your comment reminded me of how the original
Terminator apparently runs on an 8-bit MOS 6502 if you look at some of the
code flying across the screen in those red-tinted POV scenes.

~~~
CatsoCatsoCatso
Which in turn reminds me of this BBC Radio 4 clip which made me chuckle:
[http://www.bbc.co.uk/programmes/p02n9txt](http://www.bbc.co.uk/programmes/p02n9txt)

"The Terminator is an accounting programme run amok"

------
kozak
Would be great if someone reimplemented it for the web.

~~~
dspillett
Maybe it would run in something like
[https://s-macke.github.io/jor1k/demos/main.html](https://s-macke.github.io/jor1k/demos/main.html)
\- I might have to give it a try when I'm at home and have some play time...

~~~
nsxwolf
Uh wow, this is awesome.

