tonyr
Posts: 30
Joined: Mon Aug 29, 2016 2:24 pm

Use RPi as memory for processor?

Mon Nov 28, 2016 9:32 am

Hi All,

My first post - everyone has to start somewhere.

I'd like to build a processor out of transistors (e.g. see http://www.megaprocessor.com/ and others at http://members.iinet.net.au/~daveb/simp ... ghome.html). I want my RPi to act as the controller and memory. For example, let's say that want 16 bit memory in a 16 bit address space. I use:

* 16 bits for the address
* 16 bits for data in
* 16 bits for data out
* 1 bit for address ready
* 1 bit for read/write
* 1 bit for done

To write I put the address and data on the GPIO, set write mode and then set address ready. A small program in a tight loop on the RPi sees the address ready go high, it reads the address and data and stores the data at that index in an array in program space and sets done. Similarly to read the address only goes in with the mode set to read, when address ready is high the index is read, the array value is put on data out and done is set.

My main question of course is: Is this completely bonkers?

Assuming it's not altogether stupid then:

* what speed could I expect to get, am I hardware GPIO limited or does the RPi have a deep software stack that will slow me down?
* I don't have 51 GPIO pins. Is switching GPIO from input to output mode very slow?

Has anyone else done anything like this that I can learn from?


Tony

scotty101
Posts: 2333
Joined: Fri Jun 08, 2012 6:03 pm

Re: Use RPi as memory for processor?

Mon Nov 28, 2016 10:23 am

Yes. Totally bonkers.

Use a CPLD or FPGA instead.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
PeterO
Posts: 3632
Joined: Sun Jul 22, 2012 4:14 pm

Re: Use RPi as memory for processor?

Mon Nov 28, 2016 10:34 am

It's do-able, At the least you'll have to use a bidirectional bus for data. Forget using any of the GPIO libraries as they will slug performance, you'll need to go directly to the GPIO data and direction registers.

Can I ask why you don't want to use an obvious solution, a couple of 64Kx8bit static ram chips ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

tonyr
Posts: 30
Joined: Mon Aug 29, 2016 2:24 pm

Re: Use RPi as memory for processor?

Mon Nov 28, 2016 1:48 pm

I've written up my motivation at http://www.nanocpu.org/ - I want a RPi anyway to:

* Run the compiler
* Run the job queue
* Host the webpage and webcam so you can see your job running

I also want to be able to "see" the memory - that is be able to load and examine it. There may be a web page showing it all, e.g. show memory being allocated or stacks growing.

I've no problem coding assembler/C to access the GPIO registers directly. I'm more comfortable at that level than the hardware level. Is there documentation to help programming at this level?

I was planning to use the same pins for input and output, subject to the switch time being sufficiently small. I have no idea how long it takes at the moment. Maybe I should just read the docs, code it up and find out.

MarkR
Posts: 109
Joined: Fri Jan 25, 2013 1:55 pm

Re: Use RPi as memory for processor?

Mon Nov 28, 2016 1:50 pm

It sounds tricky, I doubt the Pi's GPIO is really capable of doing this at speed, especially while running other tasks. They aren't intended for the purpose.

I can, however, see the advantage, as the Pi could read/write the memory or even emulate memory-mapped IO.

Maybe you could put in some "real" SRAMs, and construct a mechanism where the Pi could address the memory somehow, for doing initial program loading and debugging. That would certainly be useful.

If you attach a Pi GPIO to your microprocessor's reset line, then Pi can hold the reset line active (low as they usually are) to prevent the micro from reading the bus, so it could assert its own addresses to read/write the ram.

However, you'd run out of gpios quickly, even a 8-bit address and 8-bit data uses up 16 gpios (not including select, clock and reset lines), I don't know if there would be enough. The Pi has about 25 usable gpios, correct?

And don't forget that the Pi needs 3v3 and is not 5v tolerant.

tonyr
Posts: 30
Joined: Mon Aug 29, 2016 2:24 pm

Re: Use RPi as memory for processor?

Tue Nov 29, 2016 12:37 pm

As the Pi will be controlling the memory clock (if not all clocks) then I'm not worried by timings. The intention is to be able to run the processor a about 1 Hz for educational purposes and as fast as it will go (I'd be very lucky to get 1 Mhz, although I would love to hit 6502 speeds). So if my processor stalls because the process on the Pi can't respond then I don't think anyone will notice.

Yes, the later RPis have ~28 GPIO pins and unfortunately that's not enough for 16 bit address and 16 bit data for SRAM. I'm sure there is a way around it.

I do appreciate that this isn't the intended purpose of the GPIO pins and I do appreciate everyone's comments here.

If I can make the design of my processor simpler by using SRAM then that's the most powerful force here. Also, if the GPIO slows my processor down, then that'll also push me to SRAM,

tonyr
Posts: 30
Joined: Mon Aug 29, 2016 2:24 pm

Re: Use RPi as memory for processor?

Wed Nov 30, 2016 8:45 am

Quick update. I've also designed myself a 4 transistor SRAM circuit. Using wiringPi I can do a read/write cycle at 200kHz on a first run model B. There are more tricks I can use, e.g. those at http://codeandlife.com/2012/07/03/bench ... pio-speed/ but at the moment I am rate limiting the clock. Anything faster than 200kHz and I get memory errors, I'm not sure if that's the GPIO not settling or a fundamental limitation of my SRAM (it is driving two LEDs with those 4 transistors).

So, when I work out what's going wrong, remove the delays, hand code the GPIO access, use my RPi3 it seems reasonable that I'll get to 1 or 2 MHz. It would be really fun to get to BBC micro speeds.

JohnCollis
Posts: 19
Joined: Mon Jun 22, 2015 7:07 pm

Re: Use RPi as memory for processor?

Wed Nov 30, 2016 10:35 am

Older 16 bit processors didn't have enough pins for separate data and address buses, they were multiplexed onto the same pins with an address/not data select pin.

The following may work, I haven't looked at any hardware or software limitations:
i/p to raspberry: 16 GPIO ports for address/data; 1 GPIO for Address/not data; 1 GPIO for data/address valid
o/p from Raspberry one GPIO for acknowledge
data/address valid and acknowledge are used as a rudimentary handshaking mechanism.
Operation:
remote processor:
Set software to detect change of state on acknowlege port
set up address on 16 GPIO ports
Set Address/not data to high to signify an address

change state on data/address valid

On Raspberry:
enable interrupt for change of level on data available port
once address read in interrupt routine
change state of acknowledge output

On remote processor:
set up data on16 GPIO pins
set Address/not data low to indicate data
change state on data/address valid

On raspberry pi, in interrupt routine
read data
change state on acknowledge pin

This would use 18 GPIO

If you wanted to make this bidirectional, add a read/not write (this would have to be with reference to either the remote processor or the Raspberry of course),1 would signify a read operation and 0 a write operation.
This would use 19 GPIO

As I said this is a rough and ready solution that may work.

tonyr
Posts: 30
Joined: Mon Aug 29, 2016 2:24 pm

Re: Use RPi as memory for processor?

Fri Dec 02, 2016 6:21 pm

Yes, I only have one bus so this is exactly what I was thinking about.

(if you are interested, the program counter is addressable as any other register, there are no jump instructions, just ALU operations on the program counter, so address and data are already very blurred).

On general progress, I'm up to 350 kHz for a read-write cycle on my 1 bit memory, but it is very dependent on my resistor values so I expect this to drop a lot when I have many more bits to deal with. I've searched and I can't find a typical maximum clock speed for Resistor Transistor Logic - every homebrew transistor based CPU I've found runs a lot slower than this. So it may well be that GPIO speed is just not an issue, if I can't get up to 1 GHz with discrete components then the GPIO will serve this just fine.

tonyr
Posts: 30
Joined: Mon Aug 29, 2016 2:24 pm

Re: Use RPi as memory for processor?

Sun Jan 01, 2017 9:21 pm

And another update. It looks like my ALU is much slower than my memory (it's got to switch a chain of 32 transistors for the ripple carry) and so I'm looking at perhaps 100 kHz access to memory. The RPi can easily do this, so not totally bonkers.

stevend
Posts: 125
Joined: Fri Oct 11, 2013 12:28 pm

Re: Use RPi as memory for processor?

Mon Jan 02, 2017 9:23 am

You can get dual port static RAM (for example) which would simplify the interface and mean that the Pi wasn't a factor in execution speed. Does make it harder to see which locations the ALU is accessing, though.

tonyr
Posts: 30
Joined: Mon Aug 29, 2016 2:24 pm

Re: Use RPi as memory for processor?

Mon Jan 02, 2017 6:45 pm

I'm using NPN BJTs for everything including a 16 bit ripple carry which means that I've got a chain of 32 transistors to switch in the ALU (both for the instruction and to increment the program counter). I'm at about 20kHz for the ALU right now, that's lots and lots of time to do memory accesses. And if something really swaps out my process, I don't care, the RPi is doing the timing and the processor (by design) will run all the way down to 0Hz so that every operation can be visualised.

Bringing the focus back to the RPi - it's a wonderful tool for building a processor. I can create as many debug signals as I like and monitor the state of whatever I want. I can also write short timing loops (I use the function below as it doesn't get optimised out) and calls to time(NULL) every ten million cycles to get frequency of operation and error rates.

I'm pretty much sold on the RPi as memory (and timing, and memory mapped IO) for my processor. It'll sit nicely on a 0.6m by 1m plywood backing and it'll be a familiar talking point to my intended audience, which is schools and universities.


void waitABit(int n) {
int i, fb = 0;

for(i = 0; i < n; i++)
fb = fb + i;
}

tonyr
Posts: 30
Joined: Mon Aug 29, 2016 2:24 pm

Re: Use RPi as memory for processor?

Wed Oct 11, 2017 7:41 am

It's taken me nearly a year, but I've just made good progress.

I bought two RPi3s and wired the 28 GPIO pins back to back. I dedicated four pins for control, each RPi3 has a RDY and ACK pins which it can write to and of course it can read the RDY and ACK pins from the other RPI3. That leaves 24 pins for IO. I wrote a small amount of C code that wrote 24 bits to the other Pi which read them, inverted them and sent them back (ask me, it won't attach). The first RPi then checks it got the bit pattern expected. Looping over all 2^24 combinations took 32 seconds, so that's 2^19 or about 500,000 read/writes per second - not bad!

Roughly speaking, my architecture needs to read and instruction and then execute it, which may involve another read/write, so the RPi shouldn't be a limitation provided my instruction rate is below 250,000 Hz. As I have a 16 bit ripple carry implemented in bipolar transistors I think I'd be lucky to get 50kHz as an instruction rate, so all is good.

BTW, the RPi will also run the emulator in parallel so I'll be constantly checking the state of the processor with a soft copy which should help debugging massively.

P.S. Yes, 28 GPIO pins was not a typo, I am using GPIO0 and GPIO1, but let's not get hung up on this as the conclusion stands whether it's 28 or 26.

User avatar
Gavinmc42
Posts: 1506
Joined: Wed Aug 28, 2013 3:31 am

Re: Use RPi as memory for processor?

Wed Oct 11, 2017 11:41 am

Have you thought about SMI mode?
Not much info out there, but SMI is common on other micros etc, designed for parallel memory access.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

tonyr
Posts: 30
Joined: Mon Aug 29, 2016 2:24 pm

Re: Use RPi as memory for processor?

Wed Oct 11, 2017 11:58 am

I haven't (for clarity I assume you mean https://en.wikipedia.org/wiki/System_Management_Mode).

My RPi is providing the clock as well as the memory, so its not critical if I get interrupted. If I'm running at 1Hz so that you can see all the flashing lights then an interrupt won't make any difference. If I'm running at top speed then the LEDs look as though they are "half on" all the time, if the interrupt was for more than 0.1s then it would manifest as a flicker, but I don't see that often enough to bother me (in what I've written so far).

User avatar
rpdom
Posts: 11707
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK

Re: Use RPi as memory for processor?

Wed Oct 11, 2017 1:48 pm

SMI = Secondary Memory Interface. A parallel memory bus running over GPIO.

tonyr
Posts: 30
Joined: Mon Aug 29, 2016 2:24 pm

Re: Use RPi as memory for processor?

Wed Oct 11, 2017 1:54 pm

Ah, interesting, thanks. I'll do some research.

Return to “Interfacing (DSI, CSI, I2C, etc.)”

Who is online

Users browsing this forum: No registered users and 15 guests