Shop OBEX P1 Docs P2 Docs Learn Events
Secret Hardware Multiplier — Parallax Forums

Secret Hardware Multiplier

dfletchdfletch Posts: 165
edited 2008-04-10 19:51 in Propeller 1
We've been hearing lots about the Propeller2 lately. One of the neat things it has is a hardware multiplier.

But Parallax has been holding out on us! There's a hidden multiplier in your Propeller 1!! It's triggered with the lovely MUL assembly instruction.

This opens us all up to a whole new world of DSP awesomeness!

Sample code that multiplies 8000 by 10000:

CON
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

VAR
  LONG a, b

OBJ
  term : "VGA_Text"

PUB start | cog

  a := 8000
  b := 10000
  term.start(16)
  term.dec(a)
  term.str(string(" * "))
  term.dec(b)
  term.str(string(" = "))
  cog := cognew(@testmul, @a)
  waitcnt(cnt + clkfreq)
  term.dec(b) ' b after multiply
  cogstop(cog)
  repeat

DAT

        ORG 0

testmul

        MOV t1, par              ' Appropriate the A parameter
        RDLONG _a, @t1           ' Provide the data
        ADD t1, #4               ' Release t1, making it a pointer to B
        RDLONG _b, @t1           ' Internalize the data
        MUL _a, _b               ' Look here <------ WOW AMAZING! Result in B.
        WRLONG _b, t1            ' Forward the data back to SPIN.
        :stop JMP #:stop         
_a      LONG 0                   ' Overshadowed A parameter.
_b      LONG 0                   ' Overshadowed B parameter.       
t1      LONG 0                   ' Loaded with parameter addresses.
        FIT 496                  ' Squeeze it all in (HA!)




Happy multiplying!

Cheers,

--fletch

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Join us in the Un-Official Propeller IRC channel: irc.freenode.net #propeller
Newbies, oldies, programmers, math professors, and everyone in-between welcome!
Propeller IRC howto
spinc - open source Spin compiler

Comments

  • JavalinJavalin Posts: 892
    edited 2008-04-01 19:02
    Good find - must check this out!

    How did you find it?

    OK - assuming this find isn't date related (1/4/2008)- its on page 419 as a reserved PASM word, and the IDE picks it up - but no documentation seems to be in the manual...... (not checked the errata!)

    But it doesn't appear to work - 10 x 10 seems to give 10, 80 x 100 gives 163.....

    J

    Post Edited (Javalin) : 4/1/2008 7:31:39 PM GMT
  • Jasper_MJasper_M Posts: 222
    edited 2008-04-01 19:49
    Wow, good work [noparse]:)[/noparse] Maybe there's a HW divider too?
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-04-01 19:53
    dfletch, you should get the result in _a not _b.
  • dfletchdfletch Posts: 165
    edited 2008-04-01 19:57
    Well stevenmess2004, this code is great at multiplying stuff by zero if I switch around those operands wink.gif

    Cheers,

    --fletch

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Join us in the Un-Official Propeller IRC channel: irc.freenode.net #propeller
    Newbies, oldies, programmers, math professors, and everyone in-between welcome!
    Propeller IRC howto
    spinc - open source Spin compiler
  • Jasper_MJasper_M Posts: 222
    edited 2008-04-01 20:00
    stevenmess2004 said...
    dfletch, you should get the result in _a not _b.

    That's why it is secret. It's probably this way because it doesn't set the flags (Z & C) correctly, so people wouldn't use it in production code. And also it doesn't seem to work with negative numbers.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-04-01 23:03
    MUL is not implemented (ie there's no dedicated logic supporting the instruction)·in the current chip, the result of the instruction is undefined.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • dfletchdfletch Posts: 165
    edited 2008-04-01 23:05
    LOL finally!

    Read the first letter of each line of ASM comments :-P

    Cheers,

    --fletch

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Join us in the Un-Official Propeller IRC channel: irc.freenode.net #propeller
    Newbies, oldies, programmers, math professors, and everyone in-between welcome!
    Propeller IRC howto
    spinc - open source Spin compiler
  • dfletchdfletch Posts: 165
    edited 2008-04-01 23:42
    OK I'm surprised nobody actually unwound what I did to make this appear to work. tpw_rules just asked me in IRC, so here's my clumsy answer (and I sorta forgot how I did this- made it a month ago).

    The value in _b is wrong. The ASM does a RDLONG of the address of the address of VAR B. The value in that HUB RAM happens to be 8000000. So I just picked 2 numbers that multiplied to it and arranged the code so MUL didn't zero the value.

    APRIL FOOLS!

    Cheers,

    --fletch

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Join us in the Un-Official Propeller IRC channel: irc.freenode.net #propeller
    Newbies, oldies, programmers, math professors, and everyone in-between welcome!
    Propeller IRC howto
    spinc - open source Spin compiler
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-04-01 23:46
    tongue.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-04-02 06:34
    Paul, when you say undefined do you mean that the answer could be anything or is it wired to some specific value or is it wired up some way that is too hard to explain?
  • Jasper_MJasper_M Posts: 222
    edited 2008-04-02 07:50
    stevenmess2004 said...
    Paul, when you say undefined do you mean that the answer could be anything or is it wired to some specific value or is it wired up some way that is too hard to explain?

    mul a,b is equivalent to mov a,#0. It sets flags accordingly (sets Z and clears C) if flags are requested.
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-04-02 08:00
    Thanks Jasper.

    dfletch, its a good thing your working on a compiler otherwise you might be sorrysmile.gif

    I think you had several of us going for awhile...
  • hippyhippy Posts: 1,981
    edited 2008-04-02 11:48
    RDLONG _a, @t1

    Intended ? Bug ? Magic ?
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-04-02 11:50
    hippy, you missed the obvious, Its an unintended feature just like Windowssmile.gif
  • dfletchdfletch Posts: 165
    edited 2008-04-02 17:30
    Haha! I almost didn't post this at all because I was convinced Hippy or PhilPi or deSilva would instantly point that out smile.gif

    Cheers,

    --fletch

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Join us in the Un-Official Propeller IRC channel: irc.freenode.net #propeller
    Newbies, oldies, programmers, math professors, and everyone in-between welcome!
    Propeller IRC howto
    spinc - open source Spin compiler
  • epmoyerepmoyer Posts: 314
    edited 2008-04-10 19:51
    Bravo! If I'd read this back when it was posted I might have had my guard up. I didn't read it until a week later so you had me on the hook! I was thinking; "ok, it must be broken in some way or they would have made it public, but maybe I can bound the operands in some way to use it only within the range over which it returns valid results.....". What a sucker! Nice one [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The World's First Open Source Guitar Pedal:··http://www.OpenStomp.com
Sign In or Register to comment.