Shop OBEX P1 Docs P2 Docs Learn Events
Memory class — Parallax Forums

Memory class

Peter VerkaikPeter Verkaik Posts: 3,956
edited 2006-06-07 06:26 in General Discussion
Hi,
While trying to optimize some classes I realized I usually define some
abstract class and extending that class for SystemRam, SystemEeprom and
I2Ceeprom to support multiple memory types. This I did for many data
classes. I reduced the number of my classes by writing a memory class

http://groups.yahoo.com/group/javelinstamp/files/Javelin%20Stamp%20IDE/lib/stamp/peripheral/memory/

So instead of an abstract class plus extended classes per datatype, I can
now define a class, passing a memory object to its constructor. Only·one
extended memory class per memory type is required. Memory objects for
SystemRam, SystemEeprom and I2C memory chips are provided, plus a
BitArray class.

The second advantage is that I·can now easily use unions (as in C, same
memory space holds different datatypes at different times) by simply passing
a memory object to some class, based upon a·value at a known location in
the memory object that serves as an union· This is useful to reduce runtime
ram requirements and also to reuse memory.

regards peter


·

Comments

  • SIPI RoboticsSIPI Robotics Posts: 4
    edited 2006-05-19 22:06
    Peter,
    Do you have any example code of using the memory class to create·unions? I was hoping to do this very thing and found your posting here.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-19 22:26
    The trick is to use Block objects. A Block object is a Memory object inside
    a Memory object.
    For example, you can define a global ram char array

    static char[noparse]/noparse myMemory = new char[noparse][[/noparse]256]; //256 bytes of ram
    static SystemRam myRam = new SystemRam(myMemory,0,256); //make it a memory object
    static Block blk1 = new Block(myRam,0,128); //first field
    static Block blk2 = new Block(myRam,128,128); //second field

    Note that myRam, blk1 and blk2 are all of type Memory and they physically
    share the same ram locations, so myRam is just like a union.

    You can use the BitArray class to access the bits of a Memory object.

    regards peter

    Post Edited (Peter Verkaik) : 5/19/2006 10:33:09 PM GMT
  • SIPI RoboticsSIPI Robotics Posts: 4
    edited 2006-06-06 23:17
    Peter,

    I have the memory classes downloaded and have instantiated the different blocks, as follows:

    ··· public static char[noparse]/noparse IOMemory = new char[noparse][[/noparse]64]; //64 bytes of ram
    ··· public static SystemRam IORam = new SystemRam(IOMemory,0,64); //make it a memory object
    ··· public static Block DOMem = new Block(IORam,17,1); //fifth field

    Now I want to assign values to them·but I·am getting error messages that either say <The type of expression "Block" is not an array type> for this code:·········DOMem[noparse][[/noparse]0]=(char)j;

    or <The type of the left-hand side"stamp/peripheral/memory/Block", is not compatible with the right hand "char"> for this code:·········DOMem=(char)j;

    I'm not clear how·I assign values to this memory and/or reference·their values?



    Thanks for your assistance.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-06-06 23:41
    You must use the read and write methods for Block,
    eg. readByte(), writeByte(), readArray() and writeArray().

    regards peter
  • SIPI RoboticsSIPI Robotics Posts: 4
    edited 2006-06-07 06:01
    Thats what I was afraid of. That's not like any union I've used. Its just a class to read/write to memory. I guess once its packed the array can be used, but I really wanted a true union (give me some pointers please!). I think defining·the array and just using predefined offsets will work better for my needs.

    ··· public static char[noparse]/noparse IOMem = new char[noparse][[/noparse]64]; //64 bytes of ram
    ··· final static int DO = 17; //fifth field
    ...

    ······· IOMem[noparse][[/noparse]DO]=(char)j;

    I am trying to use this chip for real-time control and its so slow to begin with that every added instruction and object instantiation just bogs it down further. The lure of Java got the project hooked before understanding the speed limitations of the chip.

    Thanks for your assistance to this forum.

    SIPI Robotics
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-06-07 06:26
    If your code is only for ram, then use array access directly.

    I wrote the class to support equal/simultaneous datastructures
    in system ram, system eeprom·and I2C memory using·a single class (Table classes).
    That saved a lot of code space because only the read/write
    methods are different for different memory types.

    It also allows you to define a virtual lineair memory consisting of
    different memory types. I once included the 56 ram bytes of a
    DS1307 in a 128byte array together with·system eeprom bytes.

    regards peter
Sign In or Register to comment.