To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcx.nqcOpen lugnet.robotics.rcx.nqc in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / NQC / 347
346  |  348
Subject: 
Emulating arrays in NQC
Newsgroups: 
lugnet.robotics.rcx.nqc
Date: 
Thu, 6 Jan 2000 15:50:00 GMT
Viewed: 
1811 times
  
Hi all!

as I promised earlier, I am posting the code I've written to emulate arrays. I
tried to make it general, but it's difficult to do without compiler support.
Right now it implements a 32 items array of 4 bit variables. It can be quite
easily adapted for variables of power-of-2 size.

Good luck! And feel free to come with constructive criticism!

(A note: After writing all the comments in the file, I noticed compiling time
being much larger!)
/Vlad

------------------------
/*
  ARRAY.NQC - simulating arrays of variables
    with sizes smaller than 16 bit
  (c) 2000 Vlad Dumitrescu vladdu@hotmail.com
  This code is released under the Gnu public license. Please notify
    the author if you are using this code for something meaningsful.

  History:
  version 1.0 - 2000.01.05 - Vlad Dumitrescu
    While some things have been abstracted and generalized,
      a lot has to be done manually in order to modify the present
      size of the array (32) and of the variables (4 bit). Maybe
      some support from the compiler would make that part of
      the job less tedious.
    Use only from one task! Global variables make it unsafe otherwise.
    I believe the size of the compiled code (184 + 238 bytes) cannot be
      shrunk in an impressive way. A switch statement would probably
      bring the most improvement.
    Using the array as a pool of 4 bit independent variables (ie only
      with constant indexes) could benefit in a big way from compiler
      support!

    Good luck!
*/

#define BITSPERVAR 4
#define ARRSIZE 32
#define NUMVARS (((ARRSIZE-1)*BITSPERVAR)/16+1)
#define ITEMSPERVAR (16/BITSPERVAR)

int
  arr0, arr1, arr2, arr3,
  arr4, arr5, arr6, arr7,
  arr_n, arr_val, arr_p, arr_m;

/*
  some macros that might
*/
#define get_item(n, val) { arr_n=n; sub_get_item(); val = arr_val; }
#define set_item(n, val) { arr_n=n; arr_val=val; sub_set_item(); }


/* implementation */
#define MODMASK 0x03
#define VALMASK 0x0F

#define MULTMASK0 0x0001
#define MULTMASK1 0x0010
#define MULTMASK2 0x0100
#define MULTMASK3 0x1000

sub sub_get_item()
/*
   input:
      arr_n: index of element to get -- is preserved;
   output:
      arr_val: value
*/
{
  arr_p = arr_n / ITEMSPERVAR;
  arr_val = arr0;
  if      (arr_p==1) { arr_val = arr1; }
  else if (arr_p==2) { arr_val = arr2; }
  else if (arr_p==3) { arr_val = arr3; }
  else if (arr_p==4) { arr_val = arr4; }
  else if (arr_p==5) { arr_val = arr5; }
  else if (arr_p==6) { arr_val = arr6; }
  else if (arr_p==7) { arr_val = arr7; }
  arr_p = arr_n & MODMASK;
  if      (arr_p==1) { arr_val /= 0x0010; }
  else if (arr_p==2) { arr_val /= 0x0100; }
  else if (arr_p==3) { arr_val /= 0x1000; }
  arr_val &= VALMASK;
}

void _set(int& arr)
{
  arr &= arr_m;
  arr |= arr_val;
}

#define MASK0 0xFFF0
#define MASK1 0xFF0F
#define MASK2 0xF0FF
#define MASK3 0x0FFF

sub sub_set_item()
/*
   input:
      arr_n: index of element to get -- is NOT preserved;
      arr_val: value to be set
   output:
      none
*/
{
  arr_val &= VALMASK;
  arr_p = arr_n & MODMASK;
  arr_m = MASK0;
  if      (arr_p==1) { arr_val *= 0x0010; arr_m=MASK1; }
  else if (arr_p==2) { arr_val *= 0x0100; arr_m=MASK2; }
  else if (arr_p==3) { arr_val *= 0x1000; arr_m=MASK3; }

  arr_n = arr_n / ITEMSPERVAR;
  /* arr_p can be used as temp variable here, but this will
     increase code size with 5 bytes
  */
  _set(arr0);
  if      (arr_n==1) { _set(arr1); }
  else if (arr_n==2) { _set(arr2); }
  else if (arr_n==3) { _set(arr3); }
  else if (arr_n==4) { _set(arr4); }
  else if (arr_n==5) { _set(arr5); }
  else if (arr_n==6) { _set(arr6); }
  else if (arr_n==7) { _set(arr7); }
}

/* a dummy main task */

task main()
{
  arr0=0;
  arr1=0;
  arr2=0;
  arr3=0;
  arr4=0;
  arr5=0;
  arr6=0;
  arr7=0;

  int  v;
  set_item(0, 5);
  get_item(0, v);
}



1 Message in This Thread:

Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR