Shop OBEX P1 Docs P2 Docs Learn Events
READ / WRITE to DATA - Need Example — Parallax Forums

READ / WRITE to DATA - Need Example

John KauffmanJohn Kauffman Posts: 653
edited 2010-09-21 21:19 in Propeller 1
1 - Can someone point me to a read/write example for data in RAM using SPIN?
I've searched Prop Help, manual PDF, fora and the Prop Help Tutorial. Maybe I am using the wrong search strings. I see LONG [BaseAddress] 〈[Offset]〉 in the Prop Chip Quick Reference pdf, but am not sure how to use it without an example.

2 - I'm putting the data into the RAM with the FILE command, but am having problems finding the address where FILE loads into RAM. I need that address for my first READ.

Much thanks.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-21 19:02
    All variables exist in hub RAM in Spin. That's why you don't see any read/write examples. You can declare a variable in a DAT section or in a VAR section and it's accessed the same way. If you have

    DAT
    sample LONG 0

    then you'd refer to this as the variable sample just like any other variable. It would be a LONG. If you use WORD, then it would be like any other word variable. If you use BYTE, then it would be like any other byte variable. In both cases, you'd refer to it by name.

    If you want a byte array, you could have

    DAT
    myArray BYTE "This is a string",0

    You'd refer to bytes here with myArray[subscript] where subscript is any expression. Obviously, if you refer to a byte beyond what you've defined, you'd access something higher in memory, not what you might want.

    You can use FILE to load data into a DAT section. You'd refer to the data by declaring a variable just before the FILE statement like

    DAT
    myFile BYTE
    FILE filename

    myFile is a zero length byte array that can be used to access the contents provided by the FILE statement by writing myFile[subscript]. If your file contains word or long data, you'd use WORD or LONG instead of BYTE and the subscripts would work as expected with the zeroth element being the first word or long.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2010-09-21 20:01
    Thanks, Mike that answers a number of questions

    I am still confused about what is the address of the first byte of data form the file after a command of:
    FILE myDataFile

    I see the following example in the manual. I assume that the Datafile bytes are stored higher than the string. But what is the start address of the first byte of DataFile's values? (Other than me just counting how many bytes the str variable uses).

    DAT
    Str byte "This is a data string.", 0
    Data file "Datafile.dat"

    PUB GetData | Index, Temp
    Index := 0
    repeat
    Temp := byte[Data][Index++]
    <do something with Temp>
    while Temp > 0

    Much thanks, Mike. You have been a consistent and wise help in these fora for years.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-21 20:43
    Just a heads up ... BYTE[Data][...] won't work because it will use the contents of Data as the address of what you want to reference. You could say BYTE[@Data][...]. You might even try Data[...]. I don't know how the Spin compiler treats Data. It might be considered a byte array in which case Data[...] would work fine.
  • SapiehaSapieha Posts: 2,964
    edited 2010-09-21 21:19
    Hi John Kauffma

    Thanks, Mike that answers a number of questions

    I am still confused about what is the address of the first byte of data form the file after a command of:
    FILE myDataFile

    I see the following example in the manual. I assume that the Datafile bytes are stored higher than the string. But what is the start address of the first byte of DataFile's values? (Other than me just counting how many bytes the str variable uses).

    DAT
    Str byte "This is a data string.", 0
    Data file "Datafile.dat" 'Data = (LABEL NAME) of First position in inserted file

    PUB GetData | Index, Temp
    Index := 0
    repeat
    Temp := byteCOLOR=Red][B]Data[/B][/COLOR[Index++] 'Data ---> You can't start read first position with Index++ as it will directly point to Index + 1 (If I understand it correctly)
    You need first Read Data to Temp then have Index + 1


    <do something with Temp>
    while Temp > 0

    Much thanks, Mike. You have been a consistent and wise help in these fora for years.
Sign In or Register to comment.