一个工作寄存器,一个十六地址的内存,一个四地址的堆栈.
– [n]:选择操作命令
– [w][s][a][d]:选择当前内存
– [j]:推当前内存的当前命令操作结果到堆栈(不受堆栈指针影响)
– [k]:清空当前内存
– [u]:读当前内存到工作寄存器
– [i]:写工作寄存器到当前内存
– [y]:当前内存加一
– [h]:当前内存减一
– [l]:移动堆栈指针
– [o]:读当前堆栈内容到当前内存
– [m]:忘了想要干什么了…
/* >< */
EcTextbox pbThisWorkRegister;
EcTextbox[] pbTheseMemoryRegister;
EcTextbox[] pbTheseStack;
/* >< */
HcScanLine pbTheLine;
String pbMainMessage;
int pbFocusInst;
int pbFocusMemo;
int pbFocusStack;
boolean pbIsShowBoxes;
/* >< */
int INS_Y_GRID=10;
int INS_Y_FIRST=40;
int INSINC_Y_CORD=40;
int INSDEC_Y_CORD=50;
int INSADD_Y_CORD=60;
int INSSUB_Y_CORD=70;
int INSMUL_Y_CORD=80;
int INSSHL_Y_CORD=90;
int INSLMT_Y_CORD=100;
int INSAND_Y_CORD=110;
int INSOR_Y_CORD=120;
int INS_Y_LAST=120;
/* >< */
int BLACK_A=0x000000;
int WHITE_A=0xFFFFFF;
/* >< */
int GRAY_LT=0x222222;
int GRAY_MD=0x888888;
int GRAY_HV=0xCCCCCC;
/* >< */
//@Override
public void setup() {
size(320, 240); frameRate(16); textAlign(LEFT, TOP);
frame.setTitle("Word Calculator-16bit");
/* >< */
pbTheLine = new HcScanLine(10, 2);
pbMainMessage="standby";
pbFocusInst=INS_Y_FIRST;
pbFocusMemo=0;
pbFocusStack=0;
pbIsShowBoxes=true;
/* >< */
stInitTextBoxes();
/* >< */
pbThisWorkRegister.cmIsFocused=true;
/* >< */
}//+++
//@Override
public void draw() {
background(0);
pbTheLine.ccUpdate();
/* >< */
drTextLayer(true);
drInfoBarLayer(true);
drStatusBarLayer(true);
/* >< */
drTextboxes(true);
/* >< */
}//+++
/* >< */
//@Override
public void keyPressed() {
/* >< */
if(key=='w'){pbFocusMemo-=4;pbMainMessage="[w]memry target move up";}
if(key=='s'){pbFocusMemo+=4;pbMainMessage="[s]memry target move down";}
if(key=='d'){pbFocusMemo++;pbMainMessage="[d]memry target move right";}
if(key=='a'){pbFocusMemo--;pbMainMessage="[a]memry target move left";}
pbFocusMemo=constrain(pbFocusMemo,0,15);
/* >< */
if(key=='y'){pbTheseMemoryRegister[pbFocusMemo].cmVal++;pbMainMessage="[y]increament target memory";}
if(key=='h'){pbTheseMemoryRegister[pbFocusMemo].cmVal--;pbMainMessage="[h]decreament target memory";}
/* >< */
if(key=='j'){
pbTheseStack[3].cmVal=pbTheseStack[2].cmVal;
pbTheseStack[2].cmVal=pbTheseStack[1].cmVal;
pbTheseStack[1].cmVal=pbTheseStack[0].cmVal;
pbTheseStack[0].cmVal=fnOprate(pbFocusInst,pbThisWorkRegister.cmVal,pbTheseMemoryRegister[pbFocusMemo].cmVal);
pbMainMessage="[j]oprate target memory to stack with opratecode:"+str(pbFocusInst);
}
if(key=='k'){pbTheseMemoryRegister[pbFocusMemo].cmVal=0;pbMainMessage="[k]target memory clear";}
if(key=='u'){pbThisWorkRegister.cmVal=pbTheseMemoryRegister[pbFocusMemo].cmVal;pbMainMessage="[u]read target memory to workreg";}
if(key=='i'){pbTheseMemoryRegister[pbFocusMemo].cmVal= pbThisWorkRegister.cmVal;pbMainMessage="[i]write workreg to target memory";}
/* >< */
if (key=='o') {pbTheseMemoryRegister[pbFocusMemo].cmVal=pbTheseStack[pbFocusStack].cmVal;pbMainMessage="[o]pop selected stack to target memory";}
if (key=='l') {
pbFocusStack+=1;
if(pbFocusStack>3){pbFocusStack=0;}
pbMainMessage="[l]select stack at index:"+str(pbFocusStack);
}
/* >< */
if (key=='n') {
pbFocusInst+=10;
if (pbFocusInst>INS_Y_LAST) {pbFocusInst=INS_Y_FIRST;}
pbMainMessage="[n]select oprate at code:"+str(pbFocusInst);
}
if (key=='m') {pbIsShowBoxes=!pbIsShowBoxes;pbMainMessage="[m]showElements:"+str(pbIsShowBoxes)+"<unsupported>";}//we needa think about this next time
/* >< */
if (key=='q') {exit();}
}//+++
/* >< */
class EcTextbox {
int cmCordX, cmCordY;
int cmVal;
int cmColorBack, cmColorFront;
boolean cmIsFocused;
EcTextbox(int pxCordX, int pxCordY) {
cmCordX=pxCordX;
cmCordY=pxCordY;
cmVal=27;
cmColorBack=128;
cmColorFront=32;
cmIsFocused=false;
}
/* >< */
void ccUpdate() {
noStroke();
fill(cmColorBack);
rect(cmCordX, cmCordY, 38, 16);
if (cmIsFocused) {cmColorBack=192;}
else {cmColorBack=128;}
fill(cmColorFront);
text(str(cmVal), cmCordX, cmCordY);
}
/* >< */
}//+++
/* >< */
class HcScanLine {
float cmPosY, cmSpeed;
HcScanLine (float pxY, float pxSpeed) {
cmPosY = pxY;
cmSpeed = pxSpeed;
}
void ccUpdate() {
cmPosY += cmSpeed;
if (cmPosY > height) {
cmPosY = 0;
}
stroke(200);
line(0, cmPosY, width, cmPosY);
}
}//+++
/* >< */
boolean drTextLayer(boolean pxAct){
if (pxAct==false){return false;}
fill(225);
/* >< */
text(">>", 4, pbFocusInst);
text(">>", 260, 60+pbFocusStack*20);
text("inc", 24, INSINC_Y_CORD);
text("dec", 24, INSDEC_Y_CORD);
text("add", 24, INSADD_Y_CORD);
text("sub", 24, INSSUB_Y_CORD);
text("mul", 24, INSMUL_Y_CORD);
/* >< */
text("shl", 24, INSSHL_Y_CORD);
text("lmt", 24, INSLMT_Y_CORD);
text("and", 24, INSAND_Y_CORD);
text(" or", 24, INSOR_Y_CORD);
/* >< */
text("workReg",70,50-14);
text("memory",70,90-14);
text("stack",280,60-14);
/* >< */
return true;
}//+++
/* >< */
boolean drInfoBarLayer(boolean pxAct){
if (pxAct==false){return false;}
fill(225);rect(0,0,320,18);
fill(16);text("[Q] [W][S][A][D] [J][K][U][I] [Y][H] [O][L] [N][M]", 2, 2);
return true;
}//+++
/* >< */
boolean drStatusBarLayer(boolean pxAct){
if (pxAct==false){return false;}
fill(128);rect(0,210,320,20);
fill(255);text(pbMainMessage, 2, 212);
return true;
}//+++
/* >< */
boolean drTextboxes(boolean pxAct){
if(pxAct==false){return false;}
/* >< */
pbThisWorkRegister.ccUpdate();
/* >< */
for(int i=0;i<16;i++){
if(i==pbFocusMemo){pbTheseMemoryRegister[i].cmIsFocused=true;}else{pbTheseMemoryRegister[i].cmIsFocused=false;}
pbTheseMemoryRegister[i].ccUpdate();
}
/* >< */
for(int j=0;j<4;j++){pbTheseStack[j].ccUpdate();}
/* >< */
return true;
}//+++
/* >< */
int fnOprate(int pxOpt, int pxMn, int pxSb){
if(pxOpt==INSINC_Y_CORD){return pxSb+1;}
if(pxOpt==INSDEC_Y_CORD){return pxSb-1;}
if(pxOpt==INSADD_Y_CORD){return pxMn+pxSb;}
if(pxOpt==INSSUB_Y_CORD){return pxMn-pxSb;}
if(pxOpt==INSMUL_Y_CORD){return pxMn*pxSb;}
if(pxOpt==INSSHL_Y_CORD){return pxSb/2;}
if(pxOpt==INSLMT_Y_CORD){return constrain(pxMn,0,abs(pxSb));}
if(pxOpt==INSAND_Y_CORD){return pxMn&pxSb;}
if(pxOpt==INSOR_Y_CORD){return pxMn|pxSb;}
/* >< */
return -1;
}//+++
/* >< */
boolean stInitTextBoxes(){
/* >< */
pbThisWorkRegister = new EcTextbox(70, 50);
/* >< */
pbTheseMemoryRegister = new EcTextbox[16];
for (int i=0;i<16;i++){
pbTheseMemoryRegister[i]=new EcTextbox(70+i%4*45,90+i*6-i%4*6);
}
/* >< */
pbTheseStack = new EcTextbox[4];
for (int j=0;j<4;j++){
pbTheseStack[j]=new EcTextbox(280,60+j*20);
}
return true;
}//+++
//**> <**
//**> <**
- 本文固定链接: http://iprocessing.cn/2017/09/12/习作xx_伪单片机编程器/
- 转载请注明: constrain 于 Processing编程艺术 发表