From Full Adder to a 8bit MCU - Page 1
Forum section: electronics / projects

author: Gatos De La Muerte
author_rank: Contributor
timestamp: November 03, 2013, 07:41:45 pm
content: So Last year I started working on a simple par of an 4Bit ALU that would do multiplication and over a course of a year it evolved to a "fully functional" 8bitMicrocontroller, although it has no practical use and is only at a simulation stage.Even though I'm not good at explaining my work, I'd like to share it with the few it might interest, this is not a guide for how to make a MCU, but there might be some extra information in the video descriptions for the people that are interested, I've also included two whole circuit schematics.I need to say that I'm not a professional video editor/producer and that I have dyslexia, so the grammar and poor video editing might bee too much for some people and for that I'm sorry there is only so much Google translate can doOn July 13, 2012After a few day of designing and running simulations I made this simple 4bit ALU doing Multiplicationand recorded a very bad video of it doing a three bit multiplication of 5*6:July 15, 2012I incorporated addition and subtraction:This simulation was composed of 1609 components and used up to 1.6GB of RAM, and it would often crash due to the program being out of it's memory range.Jul 16, 2012After several painful hours of "OUT OF MEMORY" errors I decided to simplify the circuit and use pre-made components like a 4bit ALU (for addition and subtraction)and counters.in the previous simulations all the addition and subtractions were mad with a collection of half adders and all the components would eat up the memory.At that point the simulation was able to run on only 500MB of RAM.July 18, 2012I tried a radially new approach to the problem of RAM usage and poor precision, during multiplications.I decided to try doing the multiplications almost "physical", by shifting the values inside a collection of SRAM chips.by doing so I was able to go down from 1.6GB to 200MB or RAM to run the simulation.Even though it would be great to be able to make the circuit at a gate lever, with multisim it was not possible because of the 1.6GB restriction.Jul 27, 2012Nine days later I made a simple division circuit that would allow me to make a semi functional "ALU" actually just an AU without the logical partsThe circuit was able to choose witch operation would output and it would do all the operations at at the same time.It's hard to see anything in those videos, but the point is that it was somewhat functional, the main problem was that division were horrible they would not giveout the correct answer if the result were not even.So after month of working, procrastination and neglect I made a revised division circuit, that I could be proud off.November 5, 2012The circuit contained only one "minor" bug that was dividing 34 with 9 (and two other combination) if would not stop dividing and eventually give out a FF as a sum,even though I fixed it afterwards I can't really recall what was the problem, if my memory isn't that bad, I think it was some where on the top with the registers.at that time I was even smart enough to take pictures of the circuit, because I realized that the videos didn't show enoughcircuit schematic:http://i49.tinypic.com/28s5tmv.pngBlock 1:http://i46.tinypic.com/20068wp.png] http://i46.tinypic.com/20068wp.pngBlock 2:http://i46.tinypic.com/34nppi8.pngBlock 3:http://i50.tinypic.com/2ch9fdd.pngBlock 4:http://i47.tinypic.com/2nlwro8.pngBlock 5:http://i46.tinypic.com/16ie0ib.pngBlock 6:http://i49.tinypic.com/23sixrn.pngBlock 6.1:http://i49.tinypic.com/34eymib.pngAfter a long break I decided to revise the division and multiplicationApril 30, 2013After trying to re revise the circuits I found them to be way to complicated and confusing.The final design was organized, easy to debug, more precise and much faster then previously being able to at up to 2MHz,limited by Multisim capability to produce such high clocks and still be able to simulate every componet precise without simulation errors.At the end it was a fully functional 8bit Alu with 4 logic and 4 math functions, it may not be much but it can now be classified as an ALUAnd I was able to reduce the total amount of components to just 626 components (using pre-made components), with the components distribution:Division - 260Multiplication - 175Add/sub - 64Xor (logic) - 16Or (logic) - 16Nand (logic) - 16And (logic) - 8Circuit schematics are available at :http://share.pho.to/2vHQr26 pictures, showing the whole design in great detail.May 5, 2013after watching a video called:See How the CPU Works In One LessonI was starting to wonder just how I could use an ALU to control/execute operations inside a CPU.I decided to make a circuit that would give me an idea of what I would need to do.based on this part of the videohttp://youtu.be/cNN_tTXABUA?t=17m34sthis is what I came up with, a crud circuit that pointed me in the right direction (I like to believe so)Even though it's manually controlled It gave me a great insight of just what I would need to do to make by my standards a working CPU.June 21, 2013Almost a year after I made my first simple multiplication circuit I was finally seeing a potentially useful circuit.It was now a 8Bit CPU that was capable of running simple instructions that I made for it.Even though it's extremely messy, it was a great feeling to see the first program run successfully on an idea that I've been working and tweaking for almost a year.I've been wondering for years how a CPU would execute instructions and control everything that a computer does, and I'm finally getting close to that answerLast month I made the step from a simple CPU to what I would call a microcontroller.October 15, 2013After over a years work (though mostly procrastinations) I could finally call this a working MCU that is functional.andThose two videos shows in my opinion that the MCU is able to take and out put values from it's IO pints and it's able to execute 14 instructions (13+1 IO instruction)The final design consist of 1089 components, 3512 pins and 1099 individual nets.The MCU can be clocked at 2MHz (restricted by the accuracy of multisim)I can't believe anybody would read that much... Thank you for enduring my horrible video editing skills and poor grammar

author: ddavidebor
author_rank: Super Contributor
timestamp: November 03, 2013, 07:51:33 pm
content: Readed!Now put it in a fpga!

author: zapta
author_rank: Super Contributor
timestamp: November 03, 2013, 08:55:24 pm
content: Very impressive, I am sure it taught you a lot.How do you write programs? Machine code? Assembler?

author: Gatos De La Muerte
author_rank: Contributor
timestamp: November 03, 2013, 09:53:18 pm
content: ThanksQuote from: zapta on November 03, 2013, 08:55:24 pmA mixture of both Assembly and Machine code.The way I write the code is by first programing a 8051 MCU using Assembly, this is because I don't want to write every single value manually.the 8051 would be the equivalent of a compiler/burner?For example to do IF A > B, I first program a 8051 MCU inside Multisim, and when the simulation runs it will load the values that are on P0 to the RAM using P2 to select the addresses and P1 to toggle Write enable.When it's done loading the values to the RAM, I've programed it to shut itself down, so the only thing that execute any values stored on the RAM is my circuit.;JUMP IF A > BCode:mov p0,#06lcall ramoutmov p0,#00lcall ramoutmov p0,#22lcall ramoutmov p0,#70lcall ramoutmov p0,#80lcall ramoutCode:ramout:mov p2,Ainc Amov p1,#07mov p1,#03retThis should be easy to understand if you know Assembly, but it does not explain why I use those values or what they actually do.The code that will be loaded and executed by my MCU is 06,00,22,70,80 where the decimal values (not hex) are:06 is the instruction it selects what type of operations we want to do, here it's the number for Jump if A > B.00 is the address that will be used for this specific operation only if A is greater then B (of course that depends on the instruction number), here it's 0 but it can vary from 0-252(252 because you can't do anything other then jump back since there is only 255 available address and a jump requires 3 address).22 is the Operation settings, here it is used to set the 8bit ALU to comparator mode.70 is the value of B and80 is the value of A.The RAMOUT routine is only used to toggle the Write enable pin on the RAM, thus loading what ever is on P0 to the RAM the the address P2 (determined by the value of A).It should bee easy to see that if I were to run this instruction/program it would always jump back to address 0 (the beginning) because A is always greater then B andit would only be one line in a high language compiler.The reason for all those values are due to design implementations and are unique to this circuit.I'm actually trying to writing a documentation/manual for this MCU, as a tutorial for somebody that asked for my help to understand how he could build a "working" CPU/MCU, I must admit finding any information for making your own CPU is impossible I couldn't find any information other then this one "How the CPU Works In One Lesson " very informative but also very abstract.The way I did it was by just trying out different designs and making my own "architecture".So too answer your question, I use Assembly to load my code, but my programs are written in machine code in a decimal form in the 8051 environment to make is easier to the eye.

author: zapta
author_rank: Super Contributor
timestamp: November 04, 2013, 01:30:16 am
content: If I understand it correclty, you are programming in machine code, the 06, 00, 22, ... is your machine code.Do you use the 8051 trick just to initialize your RAM with the program? If so, I would be very surprised if the simulation program does not support and more direct and easier approach, this is is a very general need.If you must do it with the 8051 trick, probably you can simplify it by defining with the 8051 assembly a block of literal data and then call one function to copy it. Most assembler also allow to define constants and macros so you can define them for your CPU's commands, this way you will have a pseudo assembler for your processor.

author: Gatos De La Muerte
author_rank: Contributor
timestamp: November 04, 2013, 03:01:13 am
content: Yes you understood correctly, and I've been asking around on this forum and even on the National Instrument forum for Multisim.I was asking if there was any possible way to simulate some sore of EEPROM, because in Multisim 12.1 Power Pro edition it is no possible to program or store any thing on the EERPOMs that are in the libraries, and if my program uses 254 addresses it can take up too 3-5 minutes to load the programs, making debugging a pain in the ass.Like you said there should be an easier way to do this.Quote from: zapta on November 04, 2013, 01:30:16 amCould you explain to me what that would look like?Because I don't know how I can do this, I've never learned assembly other then by Googleling the commands that I needed.What I do is keep a copy of every command in a text file, then I just edit the different values of addresses, A and B variables to mach my current programs need, but you gave me a great idea.I can't believe that not even the Ni forums couldn't even help/bother to answer meThey have a word generator, you can type up to 32 bits of data (word length) and have it outputted on the pins.http://i40.tinypic.com/2mffde1.png(too big to include here)Although this is not perfect, I'm able to first load the program by selecting "Control:Burst" with "Trigger:Internal", this will (I hope) load the program to the RAM, and when it's done it will pause the simulation, then I can select External trigger and resume the simulation, when resuming the simulation, the word generator will not do anything because it is not triggered.In a real word scenario this would be optimal as I could reload/edit the data in the RAM by simply toggling the reset button (yes it even have a reset button) .I'm gonna try out that method and see if it's any better.One great thing with using the 8051 is that I don't have to worry that much with the addresses, they are incremented by the 8051 after it's done loading the current value to the RAM.If I want to store a value in another address (other then the next) I can just use:Move A,(the new address)and on the next RAM out call it will load the value to the new A and continue loading the program from that address and up (if that makes any sense).Also I think I'm probably gonna use the 17th and 18th bit as a triggers to logically connect/disconnect the word generator form the circuit, maybe I can skip the whole Pause and Resume process by using the current SR gates circuit that I use to disable the 8051.I'll have to see if I get any performance drop/improvement, this is important since Multisim can only utilize one core of the CPU

author: zapta
author_rank: Super Contributor
timestamp: November 04, 2013, 03:42:01 am
content: >> Could you explain to me what that would look like?What Assembler program do you use? I can try to look what commands it has.(haven't done Assembly for long time. Nor promising anything ;-))Edit: looking at an arbitrary 8051 assembler from the net:1. Look at the DB command, it allows you to put specific bytes in memory. You can define a block of data with your machine code and then copy it to the similator RAM.http://plit.de/asem-51/pseudo.htm2. the ORG command allows to to place your data at an arbitrary 8051 address3. the EQU command allows you to define constants (e.g. your MCU commands).4. Look at the HERE: example herehttp://plit.de/asem-51/statemen.htmit define a constant whose value is the address of the next byte. You can have one in the beginning of your block and one at the end and use a loop to copy everything between these addresses.5. Look at macro example herehttp://plit.de/asem-51/params.htmmacro or compile time functions. For example you can define a macro (I am guessing what is your MCU's commands and the syntax below is loose):macro IS_GREATER_THAN_ZERO memory1DB MY_COMPARE, memory1 // MY_COMPARE is a const (EQU) with the compare opcode of your MCUendmA macro assembler can be very powerful and some people swear by ithttps://www.grc.com/smgassembly.htm;-)

author: Gatos De La Muerte
author_rank: Contributor
timestamp: November 04, 2013, 04:03:08 am
content: Thanks for even considering itI use the Assemble inside of Multisime 12.1 Power pro edition, it appears to be 8051 metalink assembler.This is a line that is always in a new project "$MOD51 ; This includes 8051 definitions for the Metalink assembler" so yeah I'm gonna guess it's metalink assemler.8051 cross Assembler User's Manual PDFhttp://www.xess.com/static/media/manuals/asm51.pdfIf I'm not mistaking all the commands should be on page 11 (12 in the PDF)

author: Gatos De La Muerte
author_rank: Contributor
timestamp: November 04, 2013, 04:10:22 am
content: Wow thankhttp://plit.de/asem-51/params.htmseems to be very useful.

author: amyk
author_rank: Super Contributor
timestamp: November 04, 2013, 06:40:02 am
content: For a moment I thought you'd implemented an 8051... that would be even better!QuoteThis is slightly less than the complexity of a6502, and quite a bit less than that of an 1802 (schematicshere).

author: Gatos De La Muerte
author_rank: Contributor
timestamp: November 04, 2013, 07:39:20 am
content: Quote from: amyk on November 04, 2013, 06:40:02 amThere is a lot of room for improvement on this design it's probably more complex then it needs to be, like I could remove all the IF instruction and just use the Jump IF (they do the same thing just reversed).The thing is that in the simulation there is only 1089 components, but at a gate lever there would be a lot more then just 1089 gates.I don't think multisim would be able to simulate that many gates without doing something weird, version 10 or 11 had problems simulating a "simple" 4bit ALU with adders made at a gate level.I was only be able to run the simulation once before it crashed.In Version 12.1 I can't use CTRL+Z in big simulations or else it gives an error, I have to remember the changes that I made and if the circuit doesn't work I delete them manually and try again or reload the the whole design. I might be doing something wrong though, there is probably some settings that can be tweaked/adjusted, though most CPU/MCU designs are made in VHDL and multisim is probably not the best program for this task.Although I agree that making a complete 8051 would be pretty cool, it feels very rewarding to know that I've made it from "scratch" with only a singlepictureto go on and many possible ideas that were tried and failed to eventually come to this semi useful circuit.

author: ddavidebor
author_rank: Super Contributor
timestamp: November 04, 2013, 12:37:51 pm
content: With 3000$ you can probably build it in 74xx logic

author: zapta
author_rank: Super Contributor
timestamp: November 04, 2013, 11:44:00 pm
content: Have you considered using a different simulator? About 5 years ago a used free tools from Xilinx, including a simulator, and they work pretty well. You may be able to find something more predictable and still free.Edit: it seems to still be free and maybe much better than the old version I used 5 years agohttp://www.xilinx.com/products/design-tools/ise-design-suite/ise-webpack.htmAnd here is the simulator manualhttp://www.xilinx.com/support/documentation/sw_manuals/xilinx14_1/plugin_ism.pdf

author: zapta
author_rank: Super Contributor
timestamp: November 04, 2013, 11:47:15 pm
content: Quote from: Gatos De La Muerte on November 04, 2013, 04:03:08 amI looked at the PDF at xess and it seems to have everything I mentioned in my suggestions, DB, EQU, macros, etc.

author: Gatos De La Muerte
author_rank: Contributor
timestamp: November 05, 2013, 07:35:49 am
content: Thank you ZaptaBy using macros, I've made a simple "library" 58 lines long that includes every functions and it took under 20 minutes to "convert it".This is a program that checked P0 and compared it to some value (here it's 240) and out puts a value back to P1 (here the value is 40 as declared in the second MOVE command):Code:;MOVEmov p0,#13 ;INlcall ramoutmov p0,#64 ;BANK Alcall ramoutmov p0,#00 ;VALUElcall ramout;MOVEmov p0,#13 ;INlcall ramoutmov p0,#196 ;BANK Blcall ramoutmov p0,#40 ;VALUElcall ramout;IOmov p0,#14 ;INlcall ramoutmov p0,#5 ;Alcall ramout;IF A>Bmov p0,#06 ;INlcall ramoutmov p0,#244 ;Addresslcall ramoutmov p0,#150;OPlcall ramoutmov p0,#240;Blcall ramoutmov p0,#0;A (P0)lcall ramout;JUMPmov p0,#03 ;INRlcall ramoutmov p0,#06 ;ADRESSlcall ramoutmov p0, #0 ;jumplcall ramoutmov A,#245 ;if A>B this is where it would jump else it does "no operation" until it reaches address 245;IOmov p0,#14 ;INlcall ramoutmov p0,#9 ;Blcall ramout;JUMPmov p0,#03 ;INRlcall ramoutmov p0,#06 ;ADRESSlcall ramoutmov p0, #0 ;jumplcall ramoutAnd this is how it looks now (excluding the Library)Code:MOVE 64, 00MOVE 196, 40IO 5IFAB 06, 244, 150, 240, 00JUMP 06mov A,#245IO 09JUMP 06The library contains only one single macro for every IF and JUMP since it can be controlled with the first parameter of IFAB.This will make it a easier to explain and to debug. I haven't tested it yet in the actual MCU circuit, but in a separate simulation with just the 8051 everything looks to be working correctly and I've also added a piece of code that can reprogram the RAM to undo any changes made in the RAM made by any WRITE command from my MCU, it should be faster then resetting the simulation.Thumbs up to youAlso I've been thinking of switching to VHDL simulations, but never really gotten in to it.When this project is done I'm probably going to look more into it, because I know that I can't continue using multisim if I want to do more complex/bigger circuits.Quote from: ddavidebor on November 04, 2013, 12:37:51 pmIf only I had the space and equipment for that

author: Crazy Ape
author_rank: Regular Contributor
timestamp: November 05, 2013, 07:42:56 am
content: Quote from: Gatos De La Muerte on November 05, 2013, 07:35:49 amI really think you would enjoy using FPGA's, not a lot of space required for that.

author: zapta
author_rank: Super Contributor
timestamp: November 06, 2013, 07:56:44 am
content: Quote from: Gatos De La Muerte on November 05, 2013, 07:35:49 amI am curious, what HDL language do you use now with multisim to describe your design?

author: Gatos De La Muerte
author_rank: Contributor
timestamp: November 06, 2013, 07:37:08 pm
content: Quote from: zapta on November 06, 2013, 07:56:44 amI don't, multisim has a library of components.you just place them on the sheet and wire them up by hand.also FPGAs sounds like the perfect tool for me and they aren't that expensive a much better alternative to microcontrollers