Shop OBEX P1 Docs P2 Docs Learn Events
How does the Prop do math? — Parallax Forums

How does the Prop do math?

T ChapT Chap Posts: 4,223
edited 2012-09-08 08:37 in Propeller 1
There is no point to this other than I often wonder how math works internally. If you add two numbers, where does that math take place and how? Same for mult, div, sub.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-09-07 06:48
    Multiplication and division is done by subroutine in the Spin interpreter. There is no hardware multiply or divide. You can look at the PASM subroutines in the source of the Spin interpreter if you like.

    The addition and subtraction is done in hardware in the cog. See this Wiki article for a starting point. The cog hardware fetches two numbers from wherever the instruction specifies and holds them in effectively internal registers, then the arithmetic unit does what the Wiki article describes and delivers the result a couple of nanoseconds later so it can be written back into the cog's memory.
  • BitsBits Posts: 414
    edited 2012-09-07 13:00
    T chap,

    Mike has brought up some good info to chew over, but I will add more.

    To learn how it all works start by using logic gates (make your own to learn in depth, I used diodes first, then transistors to design logic gates), then build a half adder out of said parts. It is in some fashion all microprocessor adds using gates like the ones you should make.

    Hope this helps
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-09-07 13:06
    Have a look at this:
    http://iiusatech.com/murdocca/POCA/slides/

    The 3rd slide deals with arithmetics.
  • mindrobotsmindrobots Posts: 6,506
    edited 2012-09-07 17:43
    T Chap,

    Once again, I'll put my plug in for the book "Code" by Charles Petzold. It does a wonderful job of explaining how the insides of the beast in the machine work, why binary, why two's compliment numbers, memory addressing busses, clocks, instruction decoding, etc....really good stuff, well explained.
  • kwinnkwinn Posts: 8,697
    edited 2012-09-07 18:43
    Bits wrote: »
    T chap,

    Mike has brought up some good info to chew over, but I will add more.

    To learn how it all works start by using logic gates (make your own to learn in depth, I used diodes first, then transistors to design logic gates), then build a half adder out of said parts. It is in some fashion all microprocessor adds using gates like the ones you should make.

    Hope this helps

    I have to second this advice. Take one or two '181 chips and put switches on the inputs and leds on the outputs to see how it works. This chip was used as the basis for the ALU on a lot of minicomputers.
  • rwgast_logicdesignrwgast_logicdesign Posts: 1,464
    edited 2012-09-08 01:07
    i gotta say i took mindrobots advice, checked petzolds code out and im glad i did. i didnt understand latching or multiplexing at all befor this book.

    more relevant to this thread theres a chapter where he demonstrates an adder done entirely in gates. the first few chapters are really basic but when he puts away the relays and shows how logic gates are indavidually put togather to do stuff it gets good. i thought i understood boolean logic and bit operations really well from years of c... when he builds the first 2 to 4 decoder i had to reread and trace the wires a few times, not becuase he does a bad job but becuase i just couldnt think like a hardware guy using gates till it was layed out in front of me. his explanations are simple and easy to understand the book is insightfull, now i understand how woz designed a bunch of computers on paper before he could ever get real hardware.
  • rwgast_logicdesignrwgast_logicdesign Posts: 1,464
    edited 2012-09-08 01:43
    What the heck i feel like raving about code a little more. if i didnt mention it above this book is all basically digital theory. its not an electronics or coding book he never excpects you to touch a keyboard or piece of wire. its about applying what youve learned from the book in your electronics or programs

    im not even done with this book, have alot of other reading to do too so i give code a chapter or two a night. Anyways this is how this book helped me the other day.

    so im building a little board for a 4 digit 7 segment display (like a clock). Im like u learning the prop and i thought this would be a nice simple place to start writing a driver. I start writing the driver and realise i want to add code to change the brightness, this leads me to build a board around the display usining pnp transistors to amplify the current to the 7seg without drawing more current than your suppose to from the prop. i tried with a regular npn first but it didnt boost the current enough so pnp it was.

    now heres were petzolds book creeps into my head and now into my project. the pnp transistor only comes on when the base is sent a low signal or 0v from the props data pin. Well thats all fine but the driver was written to drive the display directly and was originall outputting a hi or 3.3v to turn each digit on. Basically now my driver is sending a hi but the pnp transistor will only flip for a low. so i had a few choices i could change my spin to set outa to 0, but i didnt wanna do this becuase i wanted the driver to work with a direct connection and the amplifying board. next idea is to add a flag the user can set to invert the outputs of the amplified pins. this is reasonable but adds size to the code and we only have so much room, plus i want to use this driver as the basis for a tutorial for guys like us. then i think to code and im like well why dont i just invert my signal with a hardware inverter or another transistor in this case..

    soo ya i basically had to use a transistor that would invert my signal to amplify the display as bright as i wanted, and all i did was use a npn to invert the signal to the pnp but if i hadnt read code i probably wouldnt have thought about making a hardware not gate basically. its a good book lol. if i ever have to use gates to multiplex or transistors to add a byte ill know where to start and what to do becuase of code
  • T ChapT Chap Posts: 4,223
    edited 2012-09-08 05:35
    I understand adders and gates, what I am curious about is, if there are hardware adders in the Prop, is there just one that all cogs must share, or one per cog. I was also more trying to see how two numbers get sent to the adder, assumed to be two registers presenting a long to the adder and then a result being read back into a 'return' register.

    If the adder is just a simple set of gates, the I assume there is only two registers available to add two numbers. if x + y + z is to be added, I assume this means it first adds the first two numbers into a result, then loads the result back to one of the registers then sums the last number. This would take extra time right to add 3 numbers?
  • Heater.Heater. Posts: 21,230
    edited 2012-09-08 05:58
    Almost certainly one adder per COG. Perhaps the is one Arithmetic Logic Unit (ALU) per cog that can do addition subtraction, logical operations etc depending on the bit patterns in the opcodes.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-09-08 08:01
    Each cog has an Arithmetic Logic Unit for doing all the arithmetic and logic operations needed for the instructions. If you look at the chip, you won't find specific gates that do addition and subtraction. You'll find a (relatively) large area where 65 data bits go in one side (32 bits from one part of the cog ... the source logic, 32 bits from another part of the cog ... the destination logic, and 1 bit from the flag logic) and 34 data bits come out another side (32 bits back to the destination logic plus carry and zero results to the flag logic). The source logic takes care of presenting 32 bits of data from either the instruction register or the cog's memory. The destination logic takes care of presenting 32 bits of data from the cog's memory at the destination address and writing back 32 bits of data later. Control lines from the instruction register specify which operation is to be done whether a shift by 1 or shift by 13, AND, XOR, OR, etc.

    Note that there are two other adders per cog, each associated with the PHSx and FRQx registers of a cog counter. These are simple adders since all they do is add the contents of FRQx to PHSx which gets clocked back into PHSx either on the next system clock or some other clock depending on the counter control register contents.
  • T ChapT Chap Posts: 4,223
    edited 2012-09-08 08:37
    Thanks Mike for the explanation.
Sign In or Register to comment.