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

Virtual Memory

MagIO2MagIO2 Posts: 2,243
edited 2010-01-19 20:34 in Propeller 1
Outcome of the night:

I added some functions to FSRW. It uses the SD-card as virtual memory. Currently it's only for testing the principle. So, only long is currently supported, but if you can't wait for the finished version, feel free to add any datatype you need. In the end·my goal would be to add it directly to SDSPIPASM.

Preparation:
You need a file called swap.sys on your SD-card. It's very essential that the file is not fragmented. So, you should format the SD-card, copy swap.sys, then copy anything else you need on the card.

swap.sys can have any size you need. I started with 128MB.

FSRW.SPIN - changes:
Some minor changes in the existing code. Mount has to initialize the variables used. Popen has to return the sektor where the file swap.sys starts.
var
   long swapsektor ' this is the first sektor of file swap.sys
   long swapact    ' this is the actual sektor read into swapspace
   long swapactadr ' this is the virtual memory adress at which the actual sector starts
   long swapmod    ' flag to show if the memory has been changed
   long swapspace[noparse][[/noparse]SECTORSIZE>>2]
 
 
pub vrMemLong( adr, buffer )
  if (adr&$fffffe00) <> swapactadr
    if swapmod
      sdspi.writeblock( swapact, @swapspace )
    swapactadr:=(adr&$fffffe00)
    swapact:=swapactadr>>9+swapsektor
    sdspi.readblock( swapact, @swapspace )
    swapmod:=0
    
  long[noparse][[/noparse] buffer ]:=swapspace[noparse][[/noparse] (adr&$1fd)>>2 ]
 
 
pub vwMemLong( adr, buffer )
  if (adr&$fffffe00) <> swapactadr
    if swapmod
      sdspi.writeblock( swapact, @swapspace )
    swapactadr:=(adr&$fffffe00)
    swapact:=swapactadr>>9+swapsektor
    sdspi.readblock( swapact, @swapspace )
    
  swapspace[noparse][[/noparse] (adr&$1fd)>>2 ] := long[noparse][[/noparse] buffer ]
  swapmod:=1
 
 
pub vMemFlush
  if swapmod
    sdspi.writeblock( swapact, @swapspace )
 

pub mount(basepin) | start, sectorspercluster, reserved, rootentries, sectors
{{
'   Mount a volume.  The address passed in is passed along to the block
'   layer; see the currently used block layer for documentation.  If the
'   volume mounts, a 0 is returned, else abort is called.
......... here comes the mount-code .....
}}
   [color=red]swapsektor := popen( string("swap.sys"), "r")
   swapactadr := -1[/color]
   
   return 0
 
 
pub popen(s, mode) | i, sentinel, dirptr, freeentry
 
         ' ........

         if (mode == "r")
            frem := SECTORSIZE << clustershift
            if (frem > filesize)
               frem := filesize
            [color=red]return datablock[/color]




First parameter of the new functions is a virtual memory adress. It's adress space is given by the size of the file. Please note that currently there is no check for the adress. So, if the adress exceeds the size of the file, then this version will overwrite whatever is on the SD card.

vrMemLong will copy a long from the virtual memory to the HUB-RAM adress given as second parameter.
vwMemLong will copy a long from HUB-RAM to virtual memory.

vMemFlush is only needed, if you want to make sure that changes are written ... for example before someone switches off the prop - but who should do that ? ;o)

Have fun!



PS: No FSRW relevant things are touched, so FSRW will function in parallel. That means that you can have a file open while using virtual memory. Read directorys ... whatever

PPS: should have called the functions peek and poke ;o)


Post Edited (MagIO2) : 11/14/2009 11:40:05 PM GMT

Comments

  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2009-11-15 01:27
    OK, will I be able to use this "virtual memory" for something as high speed as a video buffer? With work could someone get the Graphics object double buffering to the SD card? Or is it not fast enough?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Computers are microcontrolled.

    Robots are microcontrolled.
    I am microcontrolled.

    But you·can·call me micro.

    Want to·experiment with the SX or just put together a cool project?
    SX Spinning light display·
    Want cheap wholesale electronic parts?
    Transistor parts wholesale
  • Cluso99Cluso99 Posts: 18,069
    edited 2009-11-15 03:16
    microcontrolled: No, it is nowhere near fast enough. There are enough problems using srams.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)
    · Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
  • Ahle2Ahle2 Posts: 1,179
    edited 2009-11-15 12:11
    microcontrolled said...
    OK, will I be able to use this "virtual memory" for something as high speed as a video buffer? With work could someone get the Graphics object double buffering to the SD card? Or is it not fast enough?

    Low bandwidth example:
    320 x 240 x 2bpp x 60fps needs a bandwidth of 1.1Mb/seconds.

    Random access writing to a SD card using FSRW isn't very fast.
    I think that rewriting the same memory segments on a SD card 60 times per second isn't a very good idea.
  • mctriviamctrivia Posts: 3,772
    edited 2009-11-15 15:15
    sd cards have a microcontroller to prevent wearing of the media. you may write to the same memmory location over and over but the controller will spread the writes over the card. still they are pretty low bandwidth and high latency.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    24 bit LCD Breakout Board coming soon. $21.99 has backlight driver and touch sensitive decoder.
  • Mike HuseltonMike Huselton Posts: 746
    edited 2009-11-15 16:21
    May I suggest the www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en539024
    This and the excellent driver obex.parallax.com/objects/475/ Mike has written would be a better solution.
    I have been including this driver on my prototypes and it works fine for both high-speed RAM and Flash RAM needs.

    Swap space and code storage would be a simple application for this driver. RAM uses a couple of the 23K256 devices,
    and code & data use a Winbond W25X16AVDAIZ 2MB (or more). Works for me wink.gif.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    JMH

    Post Edited (James Michael Huselton) : 11/15/2009 4:32:06 PM GMT
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-11-15 17:09
    Thanks James, nice to know this object.
    I was nearly sure that someone already did something similar.

    There is no doubt that it works excellent. But then I read about files agin .... that's exactly the overhead that I want to get rid of. Of course I did not check yet what the winbond driver overhead is here, but in the SD card driver the overhead was 25%. Just for reading a file sequentially. Writing will be even more overhead.

    So, my intention is more a memory based access. The programmer should know what to find in which memory location. In the end I'd like to store bitmaps and menu-structures in this memory, which most likely will not be changed anymore.

    On the other hand a set of functions can be created that's completely independend from the hardware. And you could even map different kind of hardware in different memory sections...

    Let's see where it ends ;o)
  • Mike HuseltonMike Huselton Posts: 746
    edited 2009-11-15 17:37
    I switched from the SD card to RAM for high-speed access to scratch pad RAM and Flash for mostly permanent storage of those things that you listed.
    I don't even use SD cards anymore.

    Anyway, good luck with your Prop tinkering - remember to have fun!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    JMH
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-11-22 23:05
    Here is an extract of my latest sources. Still the virtual memory uses the old FSRW. Before I can switch to FSRW 2.4 I have to do some changes on my breadboard setup, as 2.4 simply does not work. Wires seem to be to long for the high speed ;o) For sure I'll migrate my code to 2.4 when it works.

    PS: Little explaination what you find inside of the attachment:

    pub getSwap
    Returns the adress of the buffer used for swapping ... with this adress you can directly use the 512 bytes

    pub checkSektor( adr )
    Checks if the given adress is available in the currently loaded sektor. If not it's checked whether the current sektor has been modified and saves it on SD card if needed. Then the sektor which contains the adr is loaded. This is usually needed internally by the other functions, but it also makes sense in combination with getSwap.

    pub vPeek( adr )
    return the long found at the given adress

    pub vPoke( adr, val )
    set the long at the fiven adress to the value

    pub vwMemCopy( adr, buffer, len )
    copies a buffer from anywhere in HUB-RAM to the sektor. It's not allowed to exceed the 512 byte boundary. For example adr=500 then you can't copy more than 12 bytes. This restriction is not checked in the function because that would increase runtime.

    pub vwMemCmpCopy( adr, buffer, len )
    this also copies a buffer into the sektor-data, but it first checks that the data is really different. If not the sektor is not marked as 'being changed'.

    pub vrMemCopy( adr, buffer, len )
    This copies content of adr into the buffer.

    pub vrMemLong( adr, buffer )
    pub vwMemLong( adr, buffer )
    Those are the functions of the proof of concept phase ... Guess these will turn out to be useless.

    pub vMemFlush
    If you want to make sure that the data is written to SD card, call this ... In between several vwXXXX-calls it's not needed, as a sektor is written automatically in case it has been changed and is replaced by an other sector.

    pub vMemRollback
    If you don't want to save the change you did in ONE sektor.






    Post Edited (MagIO2) : 11/23/2009 7:04:47 PM GMT
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-12-05 23:58
    Wanna post my latest changes here:

    Main changes:
    1. Moved forward to FSRW 2.5 as I got it running on my setup ... 1.689 kB/s ;o)))
    2. Added slots (4) to vMem.

    A slot is so to say a window to the memory. With 4 slots it's possible to have a look at 4 different memory locations. This is usefull to avoid reload/writeback if you need to work in different memory areas at the same time. Main reason to implement that is that CassLan is currently trying out vMem for his editor project (preditor).
    But be aware, if 2 slots are used for memory areas closer than 512 bytes, things might go wrong, as currently each slot would work on it's own copy.
    Per default all slots work on the same file swap.sys, but you can also assign slots to other files.
  • Cluso99Cluso99 Posts: 18,069
    edited 2009-12-06 01:18
  • CassLanCassLan Posts: 586
    edited 2009-12-06 16:44
    Mag,

    When I run the latest slot test, I get "Yest4" printed 4 times.
    I have re-formatted my card and created a new swap just incase it was fragmented.

    Any ideas?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    NYC Area Prop Club

    Prop Forum Search (Via Google)

    ·
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-12-06 21:07
    Hi Rick!
    I'm sorry that you seem to be a beta-tester for the vMem ;o) Fixed the problem you mentioned. I tried to save some calculations by using a variable in variable space accessed by several functions. But in some cases and for some functions the code which calculates the value has not been executed.

    But I added 2 new functions to compensate your inconvenience. It's vw/rMemCopyNL. The NL (for no limit [noparse]:o[/noparse])version will work even if you cross the sector boundaries and allows to copy any size of buffers. For now you should rather switch to that. Later on I'll also provide a CmpNL version, as this will minimize write access to SD card. But comparing any size of data is to slow in SPIN. So, I'll do that when porting vMem to PASM.



    Post Edited (MagIO2) : 12/6/2009 9:13:45 PM GMT
  • evanhevanh Posts: 16,147
    edited 2009-12-06 23:26
    Umm, the opening post describes something more like a virtual block device or linear filesystem; not virtual memory.

    It isn't really virtual memory unless it's the executable's memory map you are virtualising. A virtual machine, ie: Both the Spin engine and the LMM engine, could present the right environment but, I assume, with a performance penalty to every instruction.
  • evanhevanh Posts: 16,147
    edited 2009-12-07 01:11
    Heh, what this really is is an API for random access data files. It's good you've added it to an existing filesystem module.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-12-07 04:43
    You are right ... currently ... as the current implementation only utilizes the SD card. But the overall concept is much more flexible. You could for example map the EEPROM in a dedicated address-space. Or, if you have RAM you can map that to another address-space.

    Memory in 'big processors' is also organized in pages where the OS if free to swap on disk and back as needed. That's why I've chosen the name virtual memory.
  • evanhevanh Posts: 16,147
    edited 2009-12-07 06:57
    Your method, just an offset or block number referenced into a file or another memory, is not mapping to anything other than itself let alone redirecting program accesses.

    Also, paging is not virtual memory. Paging is a means of copying and tracking data between multiple memories. Virtual memory is a means of transparently remapping a program's memory accesses - hiding things like paging and framentation from the executable.

    In a single tasking processor, eg: a Cog, there is no need for VM. The level of sharing doesn't warrant it. And what sharing there is can be satified with libraries, a filesystem and some conventions. The Hub is a little unique in that multiple programs really are sharing it but, as of yet, it's always a single application and therefore can be managed as I've listed.

    There is potential for the LMM to grow into something that can manage multiple large applications at once. Maybe there should be some thought put into the PropII for facilitating both virtual memory and memory protection under the LMM.
  • CassLanCassLan Posts: 586
    edited 2009-12-07 16:58
    Hi MagIO,

    So I switched to the latest posting of the VM version on fsrw.
    The test runs great, however when I switched all the commands in my program to the NL versions, the program hung and the SD card would no longer mount, I had to re-format on the PC.

    Would using the NL version with a len value of 1 do this?

    Rick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    NYC Area Prop Club

    Prop Forum Search (Via Google)

    ·
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-12-07 18:11
    Hmmm ... not sure ... have to check that.

    But if your len it 1 you are always safe, even if you use the old non NL functions.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-12-07 18:17
    Guess you're right:
    pub vwMemCopyNL( slot, adr, buffer, len) | lenInSector
      lenInSector := 512 <# (512-(adr&$1ff))
      repeat while len>0
    
    

    lenInSector seems to be the problem. I limited it to the max length of the rest of the buffer. But it needs an additional limit max to len.
    pub vwMemCopyNL( slot, adr, buffer, len) | lenInSector
      lenInSector := (512 <# (512-(adr&$1ff))) <# len
      repeat while len>0
    
    

    ·should help.

    Same for the vrMemCopyNL. Could the prop 2 maybe be build with a little more tolerance against programming errors??? ;o)
  • CassLanCassLan Posts: 586
    edited 2009-12-07 21:04
    That totally solved it, thanks!

    Rick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    NYC Area Prop Club

    Prop Forum Search (Via Google)

    ·
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-12-07 21:12
    I thank you for your patience!
  • CassLanCassLan Posts: 586
    edited 2010-01-19 19:14
    Hi,

    I just built a new board with a uSD card socket, and for some reason PrEditor can't mount it. When I run the fsrw2.6 tests, I can choose any block mode and they all work, I tried copying the block file and switching with the vMem version of fsrw, still no good. The uSD card is good, if I put in inside an SD Adapter·I can use it on my uther board.

    Any chance you could update vmem for 2.6? Maybe this would solve my issues, or if you have any ideas.

    Thanks,

    Rick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    NYC Area Prop Club

    Prop Forum Search (Via Google)

    ·
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-01-19 20:34
    Here you are ...

    as long as nothing has changed in the interface, it's easy to put vMem into a new FSRW version. You simply add the stuff to initialize vMem into the mountExplicit. As this will open the swap.sys to find out the first sector of the file popen has to return that sector instead of returning 0. So, that's the second change.

    Then copy all the v.... functions and variables and you're done.
Sign In or Register to comment.