PICVM


A Java-based PIC Virtual Machine, capable of emulating a fairly complete PIC10F200 with some support for PIC10F202 and the PIC10F320, written entirely on a whim during my first year as a PhD student to see how difficult it would be.

The code is very modular, with classes for the clock, memory blocks (EEPROM, ROM and RAM) and various other low-level chip structures, allowing the programmer to build chips by simply plugging the various classes together to mirror the hardware, as shown here:

public Pic10f200( int program[] )
{
super( program );
progmem = new Memory( 256, 12 ); // 256 lines of 12-bit opcode instructions
registers = new Memory( 0x00FF, 8 ); // 0x00FF registers
stack = new Stack( 2, 9 ); // Two level stack

Memory offsetMemory = (Memory)new ProxyMemory( registers, 8, 0x10 );

try
{
offsetMemory.set( 0, 0x42 );
}
catch( Exception e )
{
e.printStackTrace();
}

// Load the program in ROM
flash( program );

// Configure memory aliases for this chip
registers.setAlias( 0x00, "INDF" );
registers.setAlias( 0x01, "TMR0" );
registers.setAlias( 0x02, "PCL" );
registers.setAlias( 0x03, "STATUS" );
registers.setAlias( 0x04, "FSR" );
registers.setAlias( 0x05, "OSCCAL" );
registers.setAlias( 0x06, "GPIO" );
registers.setAlias( 0x07, "CMCON0" );

for( int i=0x08; i<0x0F; i++ )
registers.setMeta( i, new RegisterMeta(false) );

// Build a new unstruction set
instructionSet = new PicInstructionSet();
PicInstructionSet set = (PicInstructionSet)instructionSet;
set.setPC( 0x02 );
instructionSet.dump();
}

The actual emulation is only done down to the logic-level states, stopping before I started writing a full blown analogue electronics simulator underneath everything! Individual lines can be in one of four states:

public enum WireState{ LOW, RISING, HIGH, FALLING };

Which provides enough flexibility to allow accurate four-phase simulation of operations, such as the “Load address”, “Perform operation”, “Store result”, “Next instruction” cycle used in most (all?) PIC Microcontrollers.

While the VM makes no attempt to simulate other chips than the PICs, there’s nothing stopping someone from using the same base classes for an AVR simulator, (for example) as the base operations are still the same at this level.

Available on GitHub here: https://github.com/JohnVidler/PICVM

This Post Has Been Viewed 251 Times