Skip to content

Commit

Permalink
Merge pull request #30 from Host32/master
Browse files Browse the repository at this point in the history
Instrucoes orientadas a BIT
  • Loading branch information
trumae committed Jun 15, 2014
2 parents 2cb7286 + 5b15ec6 commit bc57e6b
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 19 deletions.
24 changes: 20 additions & 4 deletions unipic/src/instrucoes/BCF.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,29 @@
import unipic.Memoria;

public class BCF extends Instrucao{
/***
* Inicia a instrução
* @param comando 0100 bbbf ffff
*/
@Override
public void setup(String comando){

// ** Vide Pagina 54 do manual da PIC **
setB(Integer.parseInt(comando.substring(4,7),2)); // base binario = inteiro
setF(Integer.parseInt(comando.substring(7),2)); //
}

public void run(Memoria mem, CPU cpu){

/***
* Bit 'b' no registrador 'f' é zerado
* @param mem memória a ser alterada
* @param cpu serve para usar os registradores do processador
*/
public void run(Memoria mem, CPU cpu){
// zerando o bit
// referencia: http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/
byte dadoEditado = (byte) (mem.get(this.f) & ~( 1 << this.b ));

mem.set(this.f, dadoEditado);

// incrementa PCL
mem.setPCL((byte) (mem.getPCL() + 1));
}
}
27 changes: 21 additions & 6 deletions unipic/src/instrucoes/BSF.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@
import unipic.Memoria;

public class BSF extends Instrucao{
/***
* Inicia a instrução
* @param comando 0101 bbbf ffff
*/
@Override
public void setup(String comando){

// ** Vide Pagina 54 do manual da PIC **
setB(Integer.parseInt(comando.substring(4,7),2)); // base binario = inteiro
setF(Integer.parseInt(comando.substring(7),2)); //
}

public void run(Memoria mem, CPU cpu){

/***
* Bit 'b' no registrador 'f' é setado
* @param mem memória a ser alterada
* @param cpu serve para usar os registradores do processador
*/
public void run(Memoria mem, CPU cpu){
// zerando o bit
// referencia: http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/
byte dadoEditado = (byte) (mem.get(this.f) | ( 1 << this.b ));

mem.set(this.f, dadoEditado);

// incrementa PCL
mem.setPCL((byte) (mem.getPCL() + 1));
}

}
}
32 changes: 29 additions & 3 deletions unipic/src/instrucoes/BTFSC.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,39 @@
import unipic.Memoria;

public class BTFSC extends Instrucao{
/***
* Inicia a instrução
* @param comando 0110 bbbf ffff
*/
@Override
public void setup(String comando){

// ** Vide Pagina 54 do manual da PIC **
setB(Integer.parseInt(comando.substring(4,7),2)); // base binario = inteiro
setF(Integer.parseInt(comando.substring(7),2)); //
}

/***
* Se o bit 'b' no registrador 'f' é '0', então a próxima instrução é ignorada.
* Se o bit 'b' é '0', então a próxima instrução anexada durante o processo de
* execução de instruções atual é descartada e um NOP é executado em seu lugar,
* fazendo desta uma instrução de 2 ciclos.
* @param mem memória a ser alterada
* @param cpu serve para usar os registradores do processador
*/
public void run(Memoria mem, CPU cpu){

// variavel no formato 000x 000 que servirá par pegar o bit 'b' do 'f'
byte procurador = (byte) (0 | (1 << this.b));

// pega o bit 'procurador' em f
byte bitProcurado = (byte) (mem.get(this.f) & procurador);

// se o bit for zero, executa um NOP
if(bitProcurado == 0){
Instrucao nop = new NOP();
nop.setup("000000000000");
nop.run(mem, cpu);
}

// incrementa o PCL
mem.setPCL((byte) (mem.getPCL() + 1));
}
}
35 changes: 30 additions & 5 deletions unipic/src/instrucoes/BTFSS.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,39 @@
import unipic.Memoria;

public class BTFSS extends Instrucao{
/***
* Inicia a instrução
* @param comando 0111 bbbf ffff
*/
@Override
public void setup(String comando){

// ** Vide Pagina 54 do manual da PIC **
setB(Integer.parseInt(comando.substring(4,7),2)); // base binario = inteiro
setF(Integer.parseInt(comando.substring(7),2)); //
}

public void run(Memoria mem, CPU cpu){

}
/***
* Se o bit 'b' no registrador 'f' é '1', então a próxima instrução é ignorada.
* Se o bit 'b' é '1', então a próxima instrução anexada durante o processo de
* execução de instruções atual é descartada e um NOP é executado em seu lugar,
* fazendo desta uma instrução de 2 ciclos.
* @param mem memória a ser alterada
* @param cpu serve para usar os registradores do processador
*/
public void run(Memoria mem, CPU cpu){
// variavel no formato 000x 000 que servirá par pegar o bit 'b' do 'f'
byte procurador = (byte) (0 | (1 << this.b));

// pega o bit 'procurador' em f
byte bitProcurado = (byte) (mem.get(this.f) & procurador);

// se o bit for zero, executa um NOP
if(bitProcurado != 0){
Instrucao nop = new NOP();
nop.setup("000000000000");
nop.run(mem, cpu);
}

// incrementa o PCL
mem.setPCL((byte) (mem.getPCL() + 1));
}
}
3 changes: 2 additions & 1 deletion unipic/src/instrucoes/NOP.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public void setup(String comando){
}

public void run(Memoria mem, CPU cpu){

// incrementa PCL
mem.setPCL((byte) (mem.getPCL() + 1));
}
}

Expand Down
127 changes: 127 additions & 0 deletions unipic/src/testes/TesteBitOrientendInstructions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package testes;

import static org.junit.Assert.*;
import junit.framework.TestCase;

import org.junit.Test;

import unipic.*;
import instrucoes.*;

public class TesteBitOrientendInstructions extends TestCase{
public void testBCF(){
Memoria mem = new Memoria();
CPU cpu = new CPU();

mem.set(15, (byte)255); // endereco 15 = 1111 1111
mem.set(14, (byte)1); // endereco 14 = 0000 0001
mem.set(13, (byte)128); // endereco 13 = 1000 0000

Instrucao i1 = new BCF();
i1.setup("010010101111"); // b=5 f=15, resultado = 1110 1111

Instrucao i2 = new BCF();
i2.setup("010000001110"); // b=0 f=14, resultado = 0000 0000

Instrucao i3 = new BCF();
i3.setup("010011101101"); // b=7 f=13, resultado = 0000 0000

i1.run(mem, cpu);
if(mem.get(15) != -33){
fail("Erro na 1° instrucao, esperado: -33, obtido: "+mem.get(15));
}

i2.run(mem, cpu);
if(mem.get(14) != 0){
fail("Erro na 2° instrucao, esperado: 1, obtido: "+mem.get(14));
}

i3.run(mem, cpu);
if(mem.get(13) != 0){
fail("Erro na 3° instrucao, esperado: 0, obtido: "+mem.get(13));
}
}

public void testBSF(){
Memoria mem = new Memoria();
CPU cpu = new CPU();

mem.set(15, (byte)0); // endereco 15 = 0000 0000
mem.set(14, (byte)254); // endereco 14 = 1111 1110
mem.set(13, (byte)127); // endereco 13 = 0111 1111

Instrucao i1 = new BSF();
i1.setup("010110101111"); // b=5 f=15, resultado = 0001 0000

Instrucao i2 = new BSF();
i2.setup("010100001110"); // b=0 f=14, resultado = 1111 1111

Instrucao i3 = new BSF();
i3.setup("010111101101"); // b=7 f=13, resultado = 1111 1111

i1.run(mem, cpu);
if(mem.get(15) != 32){
fail("Erro na 1° instrucao, esperado: 16, obtido: "+mem.get(15));
}

i2.run(mem, cpu);
if(mem.get(14) != -1){
fail("Erro na 2° instrucao, esperado: -1, obtido: "+mem.get(14));
}

i3.run(mem, cpu);
if(mem.get(13) != -1){
fail("Erro na 3° instrucao, esperado: -1, obtido: "+mem.get(13));
}
}

public void testBTFSC(){
Memoria mem = new Memoria();
CPU cpu = new CPU();

Instrucao inst1 = new BTFSC();
Instrucao inst2 = new BTFSC();
Instrucao inst3 = new BTFSC();

mem.set(15, (byte)0);
mem.set(14, (byte)255);

inst1.setup("011000001111");
inst2.setup("011000001110");

inst1.run(mem, cpu);
if(mem.getPCL() != 2){
fail("A 1° instrucao não incrementou 2x o PCL");
}

inst2.run(mem, cpu);
if(mem.getPCL() != 3){
fail("A 2° instrucao incrementou 2x o PCL");
}
}

public void testBTFSS(){
Memoria mem = new Memoria();
CPU cpu = new CPU();

Instrucao inst1 = new BTFSS();
Instrucao inst2 = new BTFSS();
Instrucao inst3 = new BTFSS();

mem.set(15, (byte)0);
mem.set(14, (byte)255);

inst1.setup("011000001111");
inst2.setup("011000001110");

inst1.run(mem, cpu);
if(mem.getPCL() != 1){
fail("A 1° instrucao incrementou 2x o PCL");
}

inst2.run(mem, cpu);
if(mem.getPCL() != 3){
fail("A 2° instrucao não incrementou 2x o PCL");
}
}
}

0 comments on commit bc57e6b

Please sign in to comment.