Shop OBEX P1 Docs P2 Docs Learn Events
little project ... - Page 7 — Parallax Forums

little project ...

1234579

Comments

  • hippyhippy Posts: 1,981
    edited 2007-08-31 11:34
    According to the Propeller Manual, CNT is not zeroed on start-up/reset and contains an arbitrary value. How arbitrary that value is I do not know, it may not be arbitrary enough to give a different random number seed every start-up. Also, in 'x := startpin +||CNT?//8', there's a one in 8 chance that the result will be the same as last start-up, and if you are unlucky, the time taken to go through the loop could be giving a result which always produces the same random number each time.

    I do not know how the pseudo random number generation works on the Propeller, but it could be that the code doesn't give such a random result as desired. You could try something like ...

    x := startpin + ( ( ||CNT/256 ) // 8 )

    Or dedicate a variable to holding a random number, initialise it at start-up, then update the random number in the loop and use its latest value.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-08-31 12:29
    Set up a counter so it counts up to a certain number before looping, get the user to press a button at the start of the game and use the number in the counter as your random number seed.

    Graham
  • elecnoobelecnoob Posts: 123
    edited 2007-08-31 12:42
    the random number works that's fine at the moment with this code

    x:=startpin + ||CNT?//8

    but now i need to make the "simon" code i don't know i you know the game ..
    dira[noparse][[/noparse]0..7] := %00000000
    dira[noparse][[/noparse]8..15] := %11111111
    delay := 80_000_000                       
       delay2 := 10_000_000
       delay3 := 5_000_000
    w:=1
    z:=1
    repeat
       if ina [noparse][[/noparse]0..7]
         
        x:=startpin + ||CNT?//8
        outputreg[noparse][[/noparse]z] := x
        
        repeat w
          simon[noparse][[/noparse]w] := outputreg[noparse][[/noparse]w]
           dira[noparse][[/noparse]simon[noparse][[/noparse]w]] ~~                           
           !outa[noparse][[/noparse]simon[noparse][[/noparse]w]]               ' put the led on
           waitcnt(delay + cnt )
                                                         ' z must add by 1 so the next won't overwrite the array
           outa[noparse][[/noparse]simon[noparse][[/noparse]w]] := 0
           waitcnt(delay2 + cnt)
        w:=w+1
        z:=z+1
    
    



    but he deasn't dop what i want ..
    what it should do is ..
    make an random number (ok)
    put in an array (ok)
    make a certain led on ( ok)
    then if you klick again there must be 1 led more going on ( ok)
    but it must first be the same led and than an other ( NOT OK )like an simon game ( if you don't know it go to this site : http://www.game.nl/game126.html )

    and that is what is going wrong and i can't find the fault
  • elecnoobelecnoob Posts: 123
    edited 2007-08-31 21:51
    x:=startpin + ||CNT?//8
    about this .. i'm working with delay as you know and can the cnt in the random infect the cnt in the delaY?
    btw i have found my fault in my last post
  • rjo_rjo_ Posts: 1,825
    edited 2007-09-01 04:21
    No.

    You can't write to CNT (but Desilva probably can[noparse]:)[/noparse]

    If you want to have fun... do a little experiment... just read cnt into a variable and print that value to the screen.

    Very Strange...and quite mysterious.

    Rich
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-01 06:04
    There are longer discussions from time to time referring to this subject. Search for "shadow register".
    Writing to CNT will be the same as writing to INA.

    The situation can be better understood in machine code, where you have "dest" and "source" positions
    in an instruction wired differently to the "special registers".

    The ? is not one machine instruction but leads to a little bit unknown code in the SPIN interpreter;
    depending on that ?CNT will either produce the expected random results,
    seeded with CNT := xxx, or a value directly related to the actual CNT value.
    Note that there is a difference between using CNT directly ("linear ramp-up") and an
    additional "spreading" instruction as ? which will distribute the bits through the word...

    However it makes sense to use CNT as seed, as a (short) reset does not clear it, as has already been remarked.

    Post Edited (deSilva) : 9/1/2007 8:40:32 AM GMT
  • elecnoobelecnoob Posts: 123
    edited 2007-09-05 12:18
    ok i'm at my last game of the 5
    but at this point i need a second cog to use ..
    to generate a never ending repaet for a light that must light on/off hole the time ..
    but i don't relly understand that command ...
    can somoene give me a excplanation ?
    i've been reading and trying but i don't get it ..
    pub main 
    x:=||h?//8  
    cognew(flits,@stack)
    
    pub flits
     repeat
      dira[noparse][[/noparse] x] ~~                                      
      !outa[noparse][[/noparse]x]                         
      waitcnt(80_000-000+ cnt )
      outa[noparse][[/noparse]x] := 0
    
    



    this is what i have .. but i need to be able to get the value x in that new cog
    how do i do that ?
  • rjo_rjo_ Posts: 1,825
    edited 2007-09-05 17:08
    Have been away for a few days. Looks like progress!

    Could you post your entire code?

    Rich
  • elecnoobelecnoob Posts: 123
    edited 2007-09-05 17:14
    pub twister
    dira[noparse][[/noparse]0..7] := %00000000
    dira[noparse][[/noparse]8..15] := %11111111
     
       delay := 80_000_000
       delay2 := 10_000_000
       dira[noparse][[/noparse] 8..15] ~~                                      
       !outa[noparse][[/noparse]8..15]                         
       waitcnt(delay + cnt )
       outa[noparse][[/noparse]8..15] := 0
       waitcnt(delay2 + cnt )
      
     repeat i from 0 to 8
      outputreg[i]:=0  
     w:=8
     z:=1
     p:=0
     speler:= 1
       
      repeat w
       x:=startpin + ||h?//8
       outputreg[noparse][[/noparse]9] := x
       
       repeat i from 0 to 8
        if outputreg[noparse][[/noparse]9] == outputreg[i]
         x:=startpin + ||h?//8
         outputreg[noparse][[/noparse]9] := x
         i:=0
         p:=0
         
        else
         p:= 1
         
       if p:=1
        if speler== 1
         outputreg[noparse][[/noparse]z] := outputreg[noparse][[/noparse]9]
         dira[noparse][[/noparse]x] ~~                                      
         !outa[noparse][[/noparse]x]
        elseif speler==2
         outputreg[noparse][[/noparse]z] := outputreg[noparse][[/noparse]9]
         cognew( flits,@sqstack)
    
         
        if speler == 1
          speler := 2
        else
          speler := 1
          
          
       waitcnt(delay + cnt )
       z:=z+1
                 
      outa[noparse][[/noparse]8..15] := 0
      waitcnt(delay2 + cnt )
      
    pub flits
     repeat
      dira[noparse][[/noparse]x] ~~                                      
      !outa[noparse][[/noparse]x]                         
      waitcnt(80_000_000+ cnt )
      outa[noparse][[/noparse]x] := 0
    
    [/i][/i]
    



    this is the full code of that part for what i have so far ...
    but the cognew part .. can you excplain this to me .. i've been raidning in my manual pg 189 till 190
    but i don't really get it ..
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-05 17:27
    elecnoob said...
    ....i've been reading and trying but i don't get it .. but i need to be able to get the value x in that new cog
    how do i do that ?

    But this clearly decribed in the manual:

    pub main 
    x:=||h?//8  
    cognew(flits(x), @stack)
    
    
    


    Or just:
    pub main  
    cognew(flits(||h?//8 ), @stack)
    
    
    
  • elecnoobelecnoob Posts: 123
    edited 2007-09-05 18:31
    don't i need to put something with the pub flits ...
    something like

    pub flits |x

    or something .. because now he says that there must be a "," between the flits and (x) and he wants to leave the stack out of it ...
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-05 18:39
    Its
    PUB flits(X)
    what you need....

    But get rid of that global X first, two differentz Xes are not allowed, or call the parameter Y
    (and don't forget to change it in the body of flits to Y as well.)

    And - BTW - don't forget to look inte the extremely good manual and the SPIN tutorial from time to time...
  • elecnoobelecnoob Posts: 123
    edited 2007-09-05 18:43
    i don't get it ..
    with the 2 different xes ... i mean .;
    i need to use the x in my pub twister to be the same x as the purb flits ..
    so ... i really don't get it .;
    do i need to say that y:=x
    and use y in the pub flits ? or what ?
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-05 18:49
    Please read the sections about methods and parameters in the tutorial.
    It starts on page 104 and it's called "Exercise 4"
    I am sure it will help your understanding. I can't do it better...
  • elecnoobelecnoob Posts: 123
    edited 2007-09-05 19:01
    ok srry for my stupid question ...
    but i was reading at 189 and i didn't understood that ..
    i'm really srry

    maybe an other question ..
    i need to make code to check if a button is pushed and that it stays pushed .. how can i do that best ?
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-05 19:16
    You make a suggestion first, afterwards, I comment on it wink.gif
  • elecnoobelecnoob Posts: 123
    edited 2007-09-05 20:41
    i want to do a cognew .. but because i need a stack for that i always overwrite a piece of my program ..
    rthis is probably because my stack is to small... ( i'm a right?)
    but how do i know that i choose a stack big enouf ?
    because know ... well my programm crashes all te time .. and that's not what i wanttongue.gif
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-06 07:04
    50 should be a nice value to start with, if not take 200. If your program still crashes there will a different cause for it most likely
  • elecnoobelecnoob Posts: 123
    edited 2007-09-06 08:30
    ok the stack problem is fixed ..
    but now i have an other problem ..
    everytime you say cognew he takes the next cog to proces the program under it... ( i hope i say it right)
    but everytime i make a cog new the proces that was runing stops and will an other wil continue ...

    ok ok .. maybe i said that badly i will try again ..
    what i want to do is to put 4 led's on and of in an endless repaet
    in 4 different cog's ..
    but with me he stops the previouse led to light on/off and takes an other
    this is the code i have
    
    pub twister 
    repeat 4
    x:=||h?//8
    cognew(flits(x),@stack)
    
    pub flits(ik)
      pc.str(string(13,"ik="))
      pc.dec(ik)
    dira[noparse][[/noparse]ik]~~  
    repeat 
     !outa[noparse][[/noparse]ik]                         
     waitcnt(5_000_000+ cnt )
    
    
    
  • HarleyHarley Posts: 997
    edited 2007-09-06 15:05
    elecnoob,

    You show a repeat, but nothing is indented in your example after the 'repeat 4'.


    pub twister 
    repeat 4
    x:=||h?//8
    cognew(flits(x),@stack)
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Harley Shanko
  • elecnoobelecnoob Posts: 123
    edited 2007-09-06 15:13
    if you think al little .. you can know what i mean no ?
    it's just a typefault

    it must be
    pub twister 
     repeat 4
      x:=||h?//8
      cognew(flits(x),@stack)
    pub flits (ik)
    dira[noparse][[/noparse]ik]~~  
    repeat 
     !outa[noparse][[/noparse]ik]                         
     waitcnt(5_000_000+ cnt )
    
    
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-06 16:51
    I doubt the program you posted did what you say, but there could be a misunderstanding of course...
    (1) you did not indent the code for REPEAT 4; but I assume you did it in the IDE...
    (2) You use pc.... in parallel routines; do you think this could work???
    (3) You use the same stack for all routines. This certainly can not work. Think!
    (4) Note that the first COG will stop after it has installed the other COGs; this it not an issue, just an information for you
  • elecnoobelecnoob Posts: 123
    edited 2007-09-06 18:40
    i have this as solution ...
    but still i need to find some solution for the cognew .. but besieds that ....
    var 
    stack[noparse][[/noparse]200]
    stack2[noparse][[/noparse]200]
    stack3[noparse][[/noparse]200]
    stack4[noparse][[/noparse]200]
    
    pub twister 
    c:=1
     
    repeat 4
      x:=||h?//8
      if c ==1
         cognew(flits(x),@stack)
         c:=c+1 
       elseif c == 2
          cognew(flits(x),@stack2)
         c:=c+1 
       elseif c==3
         cognew(flits(x),@stack3)
         c:=c+1 
       elseif c==4
          cognew(flits(x),@stack4)
       
    
    pub flits (ik)
    dira[noparse][[/noparse]ik]~~  
    repeat 
     !outa[noparse][[/noparse]ik]                         
     waitcnt(5_000_000+ cnt )
    
    
    



    the cognew problem .. can i fix it with the coginit? or stops the cog also if an other cog is started up ?
  • elecnoobelecnoob Posts: 123
    edited 2007-09-06 19:25
    ok i changed the cognew's in coginit
    but now he doesn't do anaything ... this is the code i have ... i only put the code with the change the rest stays the same
        if c ==1
         coginit (1,flits(x),@stack)
         c:=c+1 
        elseif c == 2
          coginit(2,flits(x),@stack2)
         c:=c+1 
        elseif c==3
         coginit (3,flits(x),@stack3)
         c:=c+1 
        elseif c==4
          coginit(4,flits(x),@stack4)
         c:=c+1
    
    
  • elecnoobelecnoob Posts: 123
    edited 2007-09-06 20:07
    ok when i put just only 1 coginit it works like a cognew..
    so now is my question .. how can i run this program flits on 4 different cog's with 4 differnt value's in the same variable?
    or is this impossible?
  • elecnoobelecnoob Posts: 123
    edited 2007-09-06 20:38
    ok i made a second pub named flits2 so i can run an other "pub" with the same code in it ..
    i made an other stack ( stack2 ) as big as the other stack ...
    and stil it doesn't work ..
    i also first want to try this on 2 led's before i'm going to 4 .. maybe less difficult for trying

    var 
    long stack[noparse][[/noparse]200]
    long stack2[noparse][[/noparse]200]
    long x
    long h 
    long c
    
    pub twister
    c:=1
    repeat 2
     x:=||h?//8
     if c==1
         coginit (1,flits(x),@stack)
         c:=c+1
     elseif c==2
         coginit (2,flits2(x),@stack2)
    
    
    pub flits(ik)
      pc.str(string(13,"ik="))
      pc.dec(ik)
     dira[noparse][[/noparse]ik]~~
     repeat 
      !outa[noparse][[/noparse]ik]
      waitcnt(5_000_000+ cnt )
    pub flits2(jij)
      pc.str(string(13,"jij="))
      pc.dec(jij)
     dira[noparse][[/noparse]jij]~~
     repeat 
      !outa[noparse][[/noparse]jij]
      waitcnt(5_000_000+ cnt )
    
    
    



    what do i do wrong ? because i don't see it ??
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-06 21:06
    Well, this is something you have little chance to see - sorry I am answering only now.

    The main problem is, that you still use PC, which is absolutely fine (though of limited use in real parallel situations).

    Nevertheless: PC starts an assembly program in COG 1. You overwrite this COG and bingo!

    So then start with COG #2 and everything will work fine (I hope smile.gif )

    You also should return to your first version, and improve it a little bit stylistically..

    Suggestions:
    - Use a count loop: REPEAT c FROM 2 TO 5
    - Compute the start of the stacks dynamically: stack_address += 200*4
    etc, etc

    Post Edited (deSilva) : 9/6/2007 9:11:25 PM GMT
  • elecnoobelecnoob Posts: 123
    edited 2007-09-07 07:24
    that stack_adress i don't really get ...
    and now my program just runs over it.
    so for 1 or an other reason it doesn't do that coginit ...
  • elecnoobelecnoob Posts: 123
    edited 2007-09-07 07:49
    i think that that is the problem .. the stack ....
    because i debuged it with that program you gave me "propterminal"
    and there i see that he goes into the flits pub but there is some fault i don't get ..
    i will post a little piece of the debug text
    what i get :
    speler2 c
    =ik=19
    what it should be
    speler2 c=1
    ik=9

    you see the difference?
    and this is my problem .. how do i fix it ?
  • elecnoobelecnoob Posts: 123
    edited 2007-09-08 10:42
    i fixed the problem by myself .. so you don't need to react on the lst post [noparse];)[/noparse]
Sign In or Register to comment.