Hello mnse,
Thank you very much.
Let me explain why I feel I need to do this. Maybe you can point me towards a better way of doing this.
Background is: I am making a class called IC, it contains an array of registers (8 bit each).
Now these registers stores variables used by IC for its operation. A register can store multiple variables.
So I made a class ICvariables which stores parameters of a variable, like register in which it is stored, position in register, data of variable, etc.
So my class IC will have an int register array and variables of ICVariable type.
And since both register and variables store same data but in different format, thus if data in one is updated then other needs to be updated too.
So I wrote setReg() and setVar() methods.
I successfully wrote method setVar() as in this I update the variable data and also update the corresponding register.
But I am having problem in setReg() because setting register is easy but setting variables corresponding to that register is difficult. I need to access the corresponding variable.
I though I would add an string array “regContent” to store the names of variables for each register. (given below following code)
Thus I need to use these varaiable names in string to update variables.
//////////
class IC{
//Registers of IC
int[] register = new int[2]; // used to store register values corresponding to IC
//Variables of IC
//Register 0
ICVariable cp_low = new ICVariable("cp_low", 0, 7, 5,0,0);
ICVariable tsd = new ICVariable("tsd", 0, 4, 0,0,0);
//Register 1
ICVariable userid = new ICVariable("userid", 1, 7, 0,0,0);
//Functions for general use
int getReg(int index){ //returns registerData
return register[index];
}
void setReg(int index, int data){ //set the registerData and update corresponding variables
//Setting register data
register[index] = data;
//Updating variable data
// Pending
}
int getVar(ICVariable var){ //returns variableData
return var.data;
}
void setVar(ICVariable variable, int data){ //set the variableData and update corresponding register
if(data < (int)Math.pow(2,(variable.MSB - variable.LSB + 1))){ // checking if input data is less than max variable size
// Setting variable data
variable.data = data;
// Updating register data
int temp = register[variable.REGISTER];
temp = temp & (~mask(variable.MSB, variable.LSB)); //clearing the space of variable in register
temp = temp | (data << variable.LSB); //setting the varaible data in register.
register[variable.REGISTER] = temp;
} else {
println("setVar(" + variable.NAME + ", " + data + ") ERROR: input data larger than variable size");
}
}
}
////////
class ICVariable {
//Variables of IC
//Fixed parameters
final String NAME; //Name of string
final int REGISTER; //Register of this variable
final int MSB; //MSB of this variable in above register
final int LSB; //LSB of this variable in above register
final int REMAINING; //if a variable is divided in two bytes, then how many REMAINING bits it has in it's lower section
//Variable parameter
int data; //data of this variable
ICVariable(String NAME, int REGISTER, int MSB, int LSB, int REMAINING, int value){ //intializes all variable parameters with provided data
this.NAME = NAME;
this.REGISTER = REGISTER;
this.MSB = MSB;
this.LSB = LSB;
this.REMAINING = REMAINING;
this.data = value;
}
}
int mask(int msb, int lsb){ //returns the mask value with 1 from msb to lsb
int mask=0;
for(int i=lsb; i<=msb; i++){
mask = mask + (int)Math.pow(2,i);
}
return mask;
}
//Array containing variables of each register
String[][] regContent = {
{ "cp_low", "tsd" }, //Register 0
{ "userid" }, //Register 1
{ "nostop", "stopduty" }, //Register 2
};```