Author Topic: Need help starting with embedded programming  (Read 8293 times)

0 Members and 1 Guest are viewing this topic.

Offline nardev

  • Supporter
  • ****
  • Posts: 411
  • Country: ba
Re: Need help starting with embedded programming
« Reply #25 on: November 27, 2018, 09:27:59 pm »
@avogadro ako te zanima, ja imam neku Spartan E3 platformu, mislim da je skoro povucena ali je jako jako dobra za FPGA. Ima dosta periferije dodane.

Javi se, mozda da se nekako dogovorimo. LP
 

Offline avogadroTopic starter

  • Contributor
  • Posts: 34
  • Country: hr
Re: Need help starting with embedded programming
« Reply #26 on: November 28, 2018, 07:45:34 pm »
Quote
There's a project out in the Google-verse called LC3B (or the earlier version LC3) which is a hardware design project.  The given state diagram can be coded up in VHDL (or whatever) to form a functional CPU with nothing more than an interrupt driven serial port.  It has separate system and user privileges and while the instruction set is clearly RISC, there is a C compiler for the LC3 version.  I don't know if it has been upgraded for the byte oriented LC3B.  There is also an assembler for the LC3 version.

This is a pretty nice project because first you build the CPU, then you add peripherals (PWM, more serial, <whatever>) then you write bare metal code in assembly language.  In the end, you will have a pretty good handle on both hardware and embedded programming.

Yeah this is something I would be really interested in doing. Where do I start though? Just reading trough the book you posted or that H&P book? What about that MIPS R2K?
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 10091
  • Country: us
Re: Need help starting with embedded programming
« Reply #27 on: November 28, 2018, 09:17:21 pm »
Quote
There's a project out in the Google-verse called LC3B (or the earlier version LC3) which is a hardware design project.  The given state diagram can be coded up in VHDL (or whatever) to form a functional CPU with nothing more than an interrupt driven serial port.  It has separate system and user privileges and while the instruction set is clearly RISC, there is a C compiler for the LC3 version.  I don't know if it has been upgraded for the byte oriented LC3B.  There is also an assembler for the LC3 version.

This is a pretty nice project because first you build the CPU, then you add peripherals (PWM, more serial, <whatever>) then you write bare metal code in assembly language.  In the end, you will have a pretty good handle on both hardware and embedded programming.

Yeah this is something I would be really interested in doing. Where do I start though? Just reading trough the book you posted or that H&P book? What about that MIPS R2K?

There are going to be a lot of opinions on this.  Here's mine: KISS - Keep It Simple Stupid.  I like Patt and Patel because they start from the very simple basics of logic gates and work up through an entire CPU with a lot of Computer Science kinds of things mixed in.  Fact is, they don't even begin to talk about the CPU until Appendix A and it's in Appendix C (which you can find separately on the Internet) that they even start getting into design.

They give you the hardware diagram, some hardware descriptions of the control and data paths and, ultimately, a table to fill out to create microcode (which I didn't use).  They give you a diagram of the COMPLETE state diagram - just convert it to microcode or VHDL.  The information is more than sufficient.

Here is Appendix C:
http://people.cs.georgetown.edu/~squier/Teaching/HardwareFundamentals/LC3-trunk/docs/LC3-uArch-PPappendC.pdf

There dozens of videos about LC3 - just Google for 'LC3 video'.  These may be an enormous help getting started.

Read through it and see if it makes any sense at all.  Then read through it a couple of times to let it sink in.  What data is moving where?  Which MUX input is being selected when?  When you catch on, it's really kind of simple.  I suppose it is simpler if it isn't your first rodeo...

There are things you need to know how to write in VHDL.  Most important is the Finite State Machine.  The entire CPU is one gigantic FSM with some supporting processes around the outside.  There is also a serial port which connects to a PC terminal emulator like PuTTY.  You are going to subscribe to OpenCores.org and grab a serial core.  Just connect up some signals and you're good to go.

Here is just a wee bit of the FSM:
Code: [Select]
       
        case State is
            when  0     =>  -- Branch Instruction
                            if BEN = '1' then
                                NextState   <= 22;  -- branch is taken
                            else
                                NextState   <= 18;  -- branch not taken   
                            end if;                             
            when  1     =>  -- ADD Instruction
                            if Immediate = '1' then
                                SR2select <= Sext5sel;
                            else
                                SR2select <= SR2outSel;
                            end if;
                            ALUop           <= ALUadd;
                            GateBusSelect   <= GateALU;
                            LD_Reg          <= '1';
                            LD_CC           <= '1';
                            NextState       <= 18;                           
            when  2     =>  -- LD Instruction
                            Addr1Select     <= Addr1PC;
                            Addr2Select     <= Addr2Sext9;
                            MARselect       <= MARadder;
                            GateBusSelect   <= GateMARmux;
                            LD_MAR          <= '1';
                            NextState       <= 25;

Look how closely this matches the transition diagram.  I didn't invent the thing, I just copied from the diagram, substituting names here and there.

The microcode approach won't require nearly as much code but it will be much more obscure.  Particularly when making tests that result in different state transitions.  I would like to use this approach some time, I like the concept of a reconfigurable processor.

The main code is right at 1000 lines plus several support files which vary from 40 to 100 lines.  Nothing significant.

I'm not going to say the project is trivial or even easy.  Starting from ground zero, it's a lot of learning.  Start by reading through the Appendix and then asking questions.  FWIW, I built mine in a large Artix 7.  The main selection criteria is that it have enough BlockRAM to support 64k by 16b words.  This isn't a hard requirement but it does provide the maximum memory.

Why not start with the RISC-V?  It's darn difficult to build pipelined machines.  There are some great projects out there, I was reading through the TinyRV1 yesterday.  Breaking a CPU down into stages is pretty easy, accounting for the hazards like an instruction using a result that is still in the pipeline makes it really complex.

https://www.csl.cornell.edu/courses/ece4750/handouts/ece4750-T02-proc-uarch.pdf

For a first project, LC3 is more than enough.  The nice thing about it is that it has no predefined IO.  You can add whatever you like.  So, when you want to build a gadget, just plunk in  an LC3 core and you have the processor.  Add widgets as required...

Here is the book with CD.  Make sure you get the CD if you buy a used book!
https://www.amazon.com/Introduction-Computing-Systems-Gates-Beyond/dp/0072467509

Alibris has many copies of the previous version - I wouldn't go there just to save $30

There may be enough information on the Interwebs to make buying the book redundant.

« Last Edit: November 28, 2018, 09:27:10 pm by rstofer »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 10091
  • Country: us
Re: Need help starting with embedded programming
« Reply #28 on: November 28, 2018, 09:41:54 pm »
The obvious question after the CPU does something useful is "How do I use this thing?".  Good question...

When I designed the BlockRAM, I used some code by hamster_nz (a FREQUENT contributor and FPGA wizard!) to allow the system to preconfigure the BlockRAM such that the code image was loaded when the FPGA started up.  But that means you need to re-run the Vivado tool to make updates.  That's a PITA!

So, we need to go back 40 years or so.  Say to around '76 or thereabouts.  The 8080 machines had the same problem:  How to load an image.  We wrote programs called 'monitors'.  These are teletype interactive programs that would allow us to load and save images from paper tape and later audio tape and, ultimately, disks.  You write the monitor program to allow for load/save as well as various debugging operations.  THIS is the code you put in the FPGA at startup.  You reserve enough memory space for this.  It might be the upper 2 or 3k of RAM.  Then you tell the C compiler to avoid this high memory.  You also force a branch instruction at address 0x0000 or you design the CPU to start from the monitor address.  In any event, the monitor is the first thing that gets run.

From here, it is a simple matter to load and save programs via the Teletype (RS232) interface by way of a program like PuTTY.

Here is a simple 8080 monitor program written in assembly language for the 8080.  Not entirely on point but the ideas are correct:

https://github.com/jefftranter/8080/tree/master/asm/monitor

Later, after you add a compact flash and some variation on the CP/M operating system which you will have the joy of writing (but in C, not assembly language), you will just put the BIOS in high RAM and a branch in address 0x0000.  Just like we did it way back when!

In the end, this is a post-graduate course in system design.  From blinking an LED to a full blown computer in several steps.
« Last Edit: November 28, 2018, 10:07:08 pm by rstofer »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->