void act() { // get aware of what capacity the memory features memsize = memory->size(); cout << "CPU: size request served, capacity is " << memsize << " data units." << endl; // make room for local computations const unsigned char opbufsize = 10; // set this > 2! char *operate_buf = new char[opbufsize]; // initialize operate_buf with some readable (ascii) data for (char *x = operate_buf; x < operate_buf + opbufsize; x++) { *x = 48 + (random() % 43); // some in the ascii alphanum domain (circa) } // A unit operation -- store operate_buf[0] into memory at some random address // choose a random address (within memory space) to operate, warn, then write address_t operaddr = random() % memsize; cout << "CPU: will write " << operate_buf[0] << " into memory's " << operaddr << " cell." << endl; // write and read ops might cause errors... try { memory->write(operate_buf[0], operaddr); } catch (std::runtime_error &e) { cout << "CPU: an error occurred: " << e.what() << endl; return; } char tmpbuf; try { memory->read(tmpbuf, operaddr); } catch (std::runtime_error &e) { cout << "CPU: an error occurred: " << e.what() << endl; return; } cout << "CPU: memory location " << operaddr << " now holding '" << tmpbuf << "'." << endl; // A block operation -- store a bunch of data units into memory at some // random address, then, fetch some of them back operaddr = random() % (memsize - opbufsize); // advicing data, and writing to memory cout << "CPU: will write: "; for (unsigned char i = 0; i < opbufsize; i++) cout << operate_buf[i]; cout << endl; try { memory->writeblock(operate_buf, opbufsize, operaddr); } catch (std::runtime_error &e) { cout << "CPU: an error occurred: " << e.what() << endl; return; } // reading back some piece of it cout << "CPU: getting back " << opbufsize/2 << " units of data..."; char *newbuf = new char[opbufsize/2]; try { memory->readblock(newbuf, opbufsize/2, operaddr); } catch (std::runtime_error &e) { cout << "CPU: an error occurred: " << e.what() << endl; return; } cout << "CPU: got back "; for (unsigned char i = 0; i < opbufsize/2; i++) cout << newbuf[i]; cout << endl; delete[] operate_buf; // free the temporary buffer }
void act() { while (true) { cout << "MemAdaptor: waiting for requests..." << endl; // wait for a request to come while (req.read() != true) wait(); // request now accepted address_t memaddr = addr.read(); // read recipient address // SIZE request? if (memsize.read() == true) { // SIZE request! cout << "MemAdaptor: SIZE request income, processing..."; addr.write(memory->size()); cout << " done." << endl; goto act_oper_ok; } // READ request? if (r_w.read() == true) { // READ request! cout << "MemAdaptor: READ request income, processing..."; char val; try { memory->read(val, memaddr); } catch (std::runtime_error &e) { // an error occurred, reporting on the bus goto act_oper_err; } data.write(val); cout << " done." << endl; goto act_oper_ok; } // in any other case.. // WRITE request! cout << "MemAdaptor: WRITE request income, processing..."; try { memory->write(data.read(), memaddr); } catch (std::runtime_error &e) { // an error occurred, reporting on the bus goto act_oper_err; } cout << " done." << endl; goto act_oper_ok; act_oper_err: // an error happened operating the service err.write(true); while (req.read() != false) wait(); err.write(false); continue; act_oper_ok: // service went ok ack.write(true); // request served // wait for peer assertion while (req.read() != false) wait(); ack.write(false); // terminate the operation } }