1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #include <stdio.h>
#include <windows.h>
#include <string>
#include <iostream>
#include "MF.h"
using namespace std;
using std::string;
class fMemory {
public:
unsigned char bytememory[64];
unsigned int frontindex; // to store all data except signs at the beginning of chip
unsigned int backindex; // to store signs at the end of chip
unsigned int availablebit;
bool memoryexceeded;
fMemory();
unsigned char * getByteMemory();
void setByteMemory(unsigned char * b);
void initializeByteMemory();
bool getBit(unsigned int byteI, unsigned int bitI);
void setBit(unsigned int byteI, unsigned int bitI, bool b);
friend class fData;
};
fMemory::fMemory() {
frontindex = 0;
backindex = 63;
availablebit = 0;
memoryexceeded = false;
}
void connectF(unsigned char * bytememory, char * arg, unsigned char * b) {
...
}
unsigned char * fMemory::getByteMemory() {
...
}
void fMemory::setByteMemory(unsigned char * b) {
...
}
void fMemory::initializeByteMemory() {
...
}
bool fMemory::getBit(unsigned int byteI, unsigned int bitI) {
...
}
void fMemory::setBit(unsigned int byteI, unsigned int bitI, bool b) {
...
}
class fData {
static fMemory * memory; // the memory allocated for ALL the data
unsigned int dataindex; // the place of the first byte
unsigned int signbyteindex; // byte where the sign is stored
unsigned int signbitindex; // byte's bit where the sign is stored
unsigned int usedbytes; // number of bytes used
bool readwrite; // allow the reading/writing
public:
fData(string, string);
unsigned char * getValue();
void setValue(unsigned char *);
bool getSign();
void setSign(bool);
unsigned char * numberToBytes(long);
long bytesToNumber(unsigned char *);
void modifyValue(string, fData *);
friend ostream& operator<<(ostream&, fData);
};
fMemory * fData::memory;
fData::fData(string dt, string rw) {
// make data reference equal to reference
if (dt == "short")
if ((2 + memory->frontindex) < memory->backindex)
usedbytes = 2;
else
throw "memory exceeded";
else if (dt == "int")
if (4 + memory->frontindex < memory->backindex)
usedbytes = 4;
else
throw "memory exceeded";
else if (dt == "long")
if (8 + memory->frontindex < memory->backindex)
usedbytes = 8;
else
throw "memory exceeded";
dataindex = memory->frontindex;
memory->frontindex += usedbytes; // to slide the back index for next data
// allow RW or not
if (rw == "RW")
readwrite = true;
else
readwrite = false;
// store the sign at the end of the chip
signbyteindex = memory->backindex;
signbitindex = memory->availablebit;
memory->availablebit++;
if (memory->availablebit == 4) {
memory->backindex--;
memory->availablebit = 0;
}
}
unsigned char * fData::getValue() {
...
}
void fData::setValue(unsigned char * v) {
...
}
bool fData::getSign() { // true = "<0", false = ">0"
...
}
void fData::setSign(bool s) { // true = "<0", false = ">0"
...
}
long fData::bytesToNumber(unsigned char * b) {
...
}
unsigned char * fData::numberToBytes(long v) {
...
}
void fData::modifyValue(string operation, fData * f) {
...
}
ostream& operator<<(ostream &o, fData fd) {
...
}
int main (int argv, char *argc[]) {
fData * d = new fData("int", "rw");
cout << "this is a fData: " << d << "\n";
printf( "Press key.\n" );
getchar();
printf("test ok!\n");
return EXIT_SUCCESS;
} |
Partager