Starting to play with PASM
gwkaub
Posts: 10
I was playing with some simple code and got some strange results:
With above code on quick start board I get output of $79 even if I change data1 from $AA to $00 or $FF.
I can change Output mask (omask) from $FF to $F0 or $0F and it turns off upper/ lower nibble.
I call enable so I thought it will go to that label and ignore first two lines. Not so!
[data]
pub Main
repeat
cognew(@enable,0)
dat
org 0
enable mov dira, inmask
test enmsk, ina wz 'check enable
mov dira, omask
mov outa, data1 'output
jmp @enable
enmsk long %0000_0000_0100
data1 long $00aa_0000
inmask long $0000_0000
omask long $00ff_0000
[/data]
After I move label up all starts working OK. I just don't get why I get $79?
dat org 0 mov dira, inmask test enmsk, ina wz 'check enable enable mov dira, omask mov outa, data1 'output jmp @enable enmsk long %0000_0000_0100 data1 long $00aa_0000 inmask long $0000_0000 omask long $00ff_0000
With above code on quick start board I get output of $79 even if I change data1 from $AA to $00 or $FF.
I can change Output mask (omask) from $FF to $F0 or $0F and it turns off upper/ lower nibble.
I call enable so I thought it will go to that label and ignore first two lines. Not so!
[data]
pub Main
repeat
cognew(@enable,0)
dat
org 0
enable mov dira, inmask
test enmsk, ina wz 'check enable
mov dira, omask
mov outa, data1 'output
jmp @enable
enmsk long %0000_0000_0100
data1 long $00aa_0000
inmask long $0000_0000
omask long $00ff_0000
[/data]
After I move label up all starts working OK. I just don't get why I get $79?
Comments
After I move label up all starts working OK. I just don't get why I get $79?
1) You call "cognew" and give it "enable" as the address. The Prop loads code from that address into a new COG and runs it.
2) You jmp to "enable" (should be #enable) within your PASM code forming a loop.
You are calling cognew in an endless repeat loop so that will start 7 COGs with the same code and then fail repeatedly.
Clearly those two lines in front of the "enable" do not get loaded to COG and cannot be run.
Those 7 COGs will all be using the same pins so they are writing over each others outputs. I guess that is not noticed when running this program.
I think that cognew should have an @ in front of "enable"
Repeat cognew OOP! got me on that one. I was playing around and got that left In. If the first two lines of code get skipped we should see data1 or data2 out depending on Z flag.
Comment out the two lines and it works. It doesn't do what one would think.
Using @ in PASM will drive you crazy as it never seems to what users want and seems to be totally broken to me. We can talk about that when when you get to it.
Things may appear to work when you use the wrong address. But problems may show up when you add more code to your program. For example that "jmp @enable" you had may well work now. It clearly jumps somewhere in COG. But the COG is full of the 496 you loaded to it at cognew time. In you small example most of that is probably NOP. Which will execute, do nothing and eventually execution runs through all the NOPs in your COG and ends up at your "enter" label again. It works! That is until you add more stuff to your program which is no longer NOPs.
I can't remember exactly but I think Might work better.
Also, why do you only test that input on start up and not in the loop. And why change the dira after start up?
Quite so. I should have been more explicit above. Those first two instructions are never loaded to COG as I said.
Would be the more normal way to organize things.
Putting all of the suggestions together, plus some suggestions from me, should make it look something like this:
Yeah, thanks, I always get those backwards and it's a while since I wrote any PASM.
Why are you setting dira twice at start up?
Ditto that question. First of all, all pins are inputs by default when the cog starts, until set to outputs. Second, there's no such thing as separate input and output masks. There's just one mask that gets written to dira, and which defines the pin directions. Third, the state of any pin that's defined as an output can also be read via ina.
-Phil
I know we have only one DIRA ,but I was going to use 8 pins as input and output.
I just predefined two masks to switch them. Now I see set as out, but read as inputs?
Will that work or am I reading output status? PASM has some strange operations.
thanks again
gwkaub, any bit that is set in dira is an output, any bit that is clear is an input*. Reading dira will reflect which are inputs and outputs. Likewise with outa. Setting bits in outa causes the Prop to attempt to output a high, and reading outa will reflect which pins are set high or low. Note that outa only shows the intended levels on the pins. A high output shorted to ground or a low output shorted to Vcc will still be read in outa as the desired levels rather than the actual levels. Connecting pins directly to Vcc or ground is obviously something to be avoided.
Ina shows the actual levels on the pins, regardless of whether the pins are inputs or outputs.
To reiterate:
mov dira, #%1100 sets pins 3 and 2 as outputs, pins 1 and 0 remain inputs.
mov outa, #%1010 sets pins 3 and 1 as highs, and 2 and 0 as lows, but due to dira, only pins 3 and 2 are affected.
mov t1, ina copies the state of all 32 pins into t1. Assuming there aren't any shorts to Vcc or ground, bits 3 and 2 show the the output levels, all other bits show the state of any external levels on those pins. It's also typically frowned upon to leave any inputs floating; want to either bias them high or low with pullup or pulldown resistors.
* Things get more complicated when dealing with multiple cogs. A pin will be an output if any cog sets it as output in dira. A pin will be high if any cog that has it set as an output also has it set as a high in outa.
-Phil
I think that was intentional .... at one time.
One of our esteemed VIPs even said once (paraphrased) that Parallax only really needed one type of customer.
Why are not code snippets posted in the forum syntax highlighted?
There have been HTML highlighters for all kinds of languages on the net for ages. There are highlighters for Spin.
Why are they not used here?
-Phil
Was it this?
EDIT: the links to the plugins there are dead. archive.org/web/ doesn't have it either. Anyone have a copy? I want it and need the Firefox version.
[php]
dat org
entry
' mov dira, inmask ' all pins are inputs on startup
mov dira, omask ' omask sets different pins than are being looked at with enmsk, so there's no problem initializing them as outputs
:loop
test enmsk, ina wz ' check enable
if_z mov outa, data1 ' set output pattern to $AA if enable is not high
if_nz mov outa, data2
jmp #:loop
enmsk long |< 3 ' decode value 3 into 0
data1 long $AA << 16 ' shift $AA left 16 bits to $00AA_0000
data2 long $55 << 16
'inmask long 0
omask long $FF << 16
[/php]
makes things look better ... somehow ...
Enjoy!
Mike