Permalink
Browse files

implement GET_STATE

  • Loading branch information...
luto committed May 16, 2015
1 parent 29900c4 commit 3e0d16c13ecf9480f6f7d8c305939853facfadf1
Showing with 61 additions and 1 deletion.
  1. +61 −1 src/cli/yaspc.js
View
@@ -1,11 +1,13 @@
var repl = require("repl");
var net = require("net");
var zpad = require("zpad");
var async = require("async");
var fs = require("fs");
var packettypes = {
LOAD: 1,
CONTINUE: 2
CONTINUE: 2,
GET_STATE: 3
};
function send_packet(client, type, payload) {
@@ -24,6 +26,14 @@ function send_packet(client, type, payload) {
client.write(pak);
}
function read_array(data, start) {
var len = data.readUInt32LE(start);
start += 4;
var arr = new Buffer(len);
data.copy(arr, 0, start, start + len);
return arr;
}
var commands = {
help: function (params, cb) {
console.log("I am the help!");
@@ -48,6 +58,31 @@ var commands = {
send_packet(client, packettypes.CONTINUE, payload);
cb();
},
getstate: function (params, cb) {
receive_response(function (data) {
var rom = read_array(data, 0);
var ram = read_array(data, 4 + rom.length);
for(var i = 0; i < 32; i++) {
process.stdout.write("b");
process.stdout.write(i.toString());
process.stdout.write(" ");
if(i < 10) {
process.stdout.write(" ");
}
process.stdout.write("= 0x");
process.stdout.write(zpad(ram[i].toString(16), 2));
process.stdout.write(" ");
if((i + 1) % 4 === 0) {
process.stdout.write("\n");
}
}
cb();
});
send_packet(client, packettypes.GET_STATE, null);
},
exit: function (params, cb) {
process.exit(0);
}
@@ -91,3 +126,28 @@ client.on("connect", function() {
console.log("connected!");
startRepl();
});
function receive_response(func) {
var len = null;
var offset = 0;
var alldata = null;
function handledata(data) {
if(alldata === null) {
len = data.readUInt32LE(0);
alldata = new Buffer(len);
data.copy(alldata, offset, 4);
offset += data.length - 4;
} else {
data.copy(alldata, offset);
offset += data.length;
}
if(offset === len) {
client.removeListener("data", handledata);
func(alldata);
}
}
client.on("data", handledata);
}

0 comments on commit 3e0d16c

Please sign in to comment.