//
// picsim.h - PIC C simulator for embedding pic microcode into a C program
//
// Copyright (C) 2006 Nathan (Acorn) Pooley
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License (gpl.txt) for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// You can contact the author, Nathan (Acorn) Pooley, by writing
// to Nathan (Acorn) Pooley, 949 Buckeye Drive, Sunnyvale, CA 94086, USA
// or through the form at http://www.rawbw.com/~acorn/wand/feedback.html
//
//#@DOC@ PIC C simulator for embedding pic microcode into a C program
//###########################################################################
//############################### DEFINES ###################################
//###########################################################################
//
// Define a label for branch or goto
//
#define picLabel(label) label: picInst(0,0,PIC_LABEL,#label)
//
// define a constant
//
#define picEqu(var,val) enum { var = val };
//
// Declare/define a register
//
#if 0
//
// Register declarations
//
// picRegDecl() - declare reg & place in local variable of same name
// picRegFind() - declare reg & return id
// picRegGlobal() - following regs are globals
// picRegLocal() - namespace for following registers
// picRegShare() - indicate 2 Local namespaces which cannot overlap
//
#define picRegDecl(name) int name = picRegFind(picCurChip, #name)
#define picRegGlobal()
#define picRegLocal(namespace)
#define picRegShare(namespace1,namespace2)
#endif
//
// Register declarations
//
// picRegLocal() - local register variable
// picRegReturn() - function return value
// picRegParam() - function parameter (pass data into function)
// picRegInOutParam() - function parameter & return value
// picRegGlobal() - global register declaration (specify namespace)
//
// picRegId() - find id of register
//
#define picRegLocal(name) name = picRegFind(picCurChip, #name, \
picNamespaceLocal)
#define picRegReturn(name) name = picRegFind(picCurChip, #name, \
picNamespaceReturn)
#define picRegParam(name) name = picRegFind(picCurChip, #name, \
picNamespaceParam)
#define picRegInOutParam(name) name = picRegFind(picCurChip, #name, \
picNamespaceInOutParam)
#define picRegGlobal(name,namespace) name = picRegFind(picCurChip, #name, \
namespace)
#define picRegId(name) picRegFind(picCurChip, #name, picNamespaceInOutParam)
//
// HIGH and LOW operators
//
#define UPPER(val) (((val) >> 16) & 0xff)
#define HIGH(val) (((val) >> 8) & 0xff)
#define LOW(val) (((val) ) & 0xff)
//
// Shortcuts for getting reg value
//
#define picRV(id) picRegValU(0,id)
#define picRV16(idl,idh) picRegValU32(0,idl,idh,-1,-1)
#define picRV24(idl,idh,idu) picRegValU32(0,idl,idh,idu,-1)
#define picW() picRegValU(0,WREG)
#define picS() picRegValU(0,STATUS)
#define picFSR0() picRV16(FSR0L,FSR0H)
#define picFSR1() picRV16(FSR1L,FSR1H)
#define picFSR2() picRV16(FSR2L,FSR2H)
//
// instruction set macros
//
#define nop() picInst(0,0,PIC_NOP,0)
#define addwf(reg,dst) picInst(reg,PICDST_##dst,PIC_ADDWF,0)
#define addwfc(reg,dst) picInst(reg,PICDST_##dst,PIC_ADDWFC,0)
#define andwf(reg,dst) picInst(reg,PICDST_##dst,PIC_ANDWF,0)
#define comf(reg,dst) picInst(reg,PICDST_##dst,PIC_COMF,0)
#define decf(reg,dst) picInst(reg,PICDST_##dst,PIC_DECF,0)
#define incf(reg,dst) picInst(reg,PICDST_##dst,PIC_INCF,0)
#define iorwf(reg,dst) picInst(reg,PICDST_##dst,PIC_IORWF,0)
#define rlcf(reg,dst) picInst(reg,PICDST_##dst,PIC_RLCF,0)
#define rlncf(reg,dst) picInst(reg,PICDST_##dst,PIC_RLNCF,0)
#define rrcf(reg,dst) picInst(reg,PICDST_##dst,PIC_RRCF,0)
#define rrncf(reg,dst) picInst(reg,PICDST_##dst,PIC_RRNCF,0)
#define subfwb(reg,dst) picInst(reg,PICDST_##dst,PIC_SUBFWB,0)
#define subwf(reg,dst) picInst(reg,PICDST_##dst,PIC_SUBWF,0)
#define subwfb(reg,dst) picInst(reg,PICDST_##dst,PIC_SUBWFB,0)
#define swapf(reg,dst) picInst(reg,PICDST_##dst,PIC_SWAPF,0)
#define xorwf(reg,dst) picInst(reg,PICDST_##dst,PIC_XORWF,0)
#define movf(reg,dst) picInst(reg,PICDST_##dst,PIC_MOVF,0)
#define decfsz(reg,dst) picInst(reg,PICDST_##dst,PIC_DECFSZ,0)
#define dcfsnz(reg,dst) picInst(reg,PICDST_##dst,PIC_DCFSNZ,0)
#define incfsz(reg,dst) picInst(reg,PICDST_##dst,PIC_INCFSZ,0)
#define infsnz(reg,dst) picInst(reg,PICDST_##dst,PIC_INFSNZ,0)
#define movwf(reg) picInst(reg,0,PIC_MOVWF,0)
#define movff(src,dst) do { picInst(src,0,PIC_MOVFFa,0); \
picInst(dst,0,PIC_MOVFFb,0); } while(0)
#define cpfseq(reg) picInst(reg,0,PIC_CPFSEQ,0)
#define cpfsgt(reg) picInst(reg,0,PIC_CPFSGT,0)
#define cpfslt(reg) picInst(reg,0,PIC_CPFSLT,0)
#define tstfsz(reg) picInst(reg,0,PIC_TSTFSZ,0)
#define clrf(reg) picInst(reg,0,PIC_CLRF,0)
#define negf(reg) picInst(reg,0,PIC_NEGF,0)
#define setf(reg) picInst(reg,0,PIC_SETF,0)
#define mulwf(reg) picInst(reg,0,PIC_MULWF,0)
#define bcf(reg,bit) picInst(reg,PICDST_BIT##bit,PIC_BCF,#bit)
#define bsf(reg,bit) picInst(reg,PICDST_BIT##bit,PIC_BSF,#bit)
#define btfsc(reg,bit) picInst(reg,PICDST_BIT##bit,PIC_BTFSC,#reg "." #bit)
#define btfss(reg,bit) picInst(reg,PICDST_BIT##bit,PIC_BTFSS,#reg "." #bit)
#define btg(reg,bit) picInst(reg,PICDST_BIT##bit,PIC_BTG,#bit)
#define bc(label) if (picInst(0,0,PIC_BC,#label)) goto label
#define bn(label) if (picInst(0,0,PIC_BN,#label)) goto label
#define bnc(label) if (picInst(0,0,PIC_BNC,#label)) goto label
#define bnn(label) if (picInst(0,0,PIC_BNN,#label)) goto label
#define bnov(label) if (picInst(0,0,PIC_BNOV,#label)) goto label
#define bnz(label) if (picInst(0,0,PIC_BNZ,#label)) goto label
#define bov(label) if (picInst(0,0,PIC_BOV,#label)) goto label
#define bz(label) if (picInst(0,0,PIC_BZ,#label)) goto label
#define bra(label) if (picInst(0,0,PIC_BRA,#label)) goto label
#define goto(label) if (picInst(0,0,PIC_GOTO,#label)) goto label
#define call(func) if (picInst(0,0,PIC_CALL,#func)) func()
#define rcall(func) if (picInst(0,0,PIC_RCALL,#func)) func()
#define retfie(x) if (picInst(0,x,PIC_RETFIE,0)) return
#define retlw(val) if (picInst(0,val,PIC_RETLW,#val)) return
#define picReturn() if (picInst(0,0,PIC_RETURN,0)) return
#define reset() picInst(0,0,PIC_RESET,0)
#define sleep() picInst(0,0,PIC_SLEEP,0)
#define clrwdt() picInst(0,0,PIC_CLRWDT,0)
#define daw() picInst(0,0,PIC_DAW,0)
#define pop() picInst(0,0,PIC_POP,0)
#define push() picInst(0,0,PIC_PUSH,0)
#define lfsr(reg,val) do { picInst(reg##L,val,PIC_LFSRa,#val); \
picInst(reg##H,val,PIC_LFSRb,0); } while(0)
#define movlb(val) picInst(0,val,PIC_MOVLB,0)
#define addlw(val) picInst(0,val,PIC_ADDLW,0)
#define andlw(val) picInst(0,val,PIC_ANDLW,0)
#define iorlw(val) picInst(0,val,PIC_IORLW,0)
#define movlw(val) picInst(0,val,PIC_MOVLW,0)
#define mullw(val) picInst(0,val,PIC_MULLW,0)
#define sublw(val) picInst(0,val,PIC_SUBLW,0)
#define xorlw(val) picInst(0,val,PIC_XORLW,0)
#define tblrd() picInst(0,0,PIC_TBLRD,0)
#define tblrd_postinc() picInst(0,0,PIC_TBLRD_POSTINC,0)
#define tblrd_postdec() picInst(0,0,PIC_TBLRD_POSTDEC,0)
#define tblrd_preinc() picInst(0,0,PIC_TBLRD_PREINC,0)
#define tblwt() picInst(0,0,PIC_TBLWT,0)
#define tblwt_postinc() picInst(0,0,PIC_TBLWT_POSTINC,0)
#define tblwt_postdec() picInst(0,0,PIC_TBLWT_POSTDEC,0)
#define tblwt_preinc() picInst(0,0,PIC_TBLWT_PREINC,0)
#define picInst(reg, dst, op, str) picInstDB(reg,dst,op,__FILE__,__LINE__,str)
//###########################################################################
//############################### TYPEDEFS ##################################
//###########################################################################
typedef struct picRegRec picReg;
typedef struct picChipRec picChip;
//
// Destinations
//
typedef enum picDestEnum {
PICDST_NONE,
PICDST_W,
PICDST_F,
PICDST_BIT0,
PICDST_BIT1,
PICDST_BIT2,
PICDST_BIT3,
PICDST_BIT4,
PICDST_BIT5,
PICDST_BIT6,
PICDST_BIT7,
PICDST_READF,
PICDST_CNT,
PICDST_w = PICDST_W,
PICDST_f = PICDST_F,
PICDST_BITC = PICDST_BIT0,
PICDST_BITDC = PICDST_BIT1,
PICDST_BITZ = PICDST_BIT2,
PICDST_BITOV = PICDST_BIT3,
PICDST_BITN = PICDST_BIT4,
PICDST_END
} picDest;
//
// opcodes
//
#define PIC_OPCODES(f) \
f(NOP) \
f(ADDWF) \
f(ADDWFC) \
f(ANDWF) \
f(CLRF) \
f(COMF) \
f(CPFSEQ) \
f(CPFSGT) \
f(CPFSLT) \
f(DECF) \
f(DECFSZ) \
f(DCFSNZ) \
f(INCF) \
f(INCFSZ) \
f(INFSNZ) \
f(IORWF) \
f(MOVF) \
f(MOVFFa) \
f(MOVFFb) \
f(MOVWF) \
f(MULWF) \
f(NEGF) \
f(RLCF) \
f(RLNCF) \
f(RRCF) \
f(RRNCF) \
f(SETF) \
f(SUBFWB) \
f(SUBWF) \
f(SUBWFB) \
f(SWAPF) \
f(TSTFSZ) \
f(XORWF) \
f(BCF) \
f(BSF) \
f(BTFSC) \
f(BTFSS) \
f(BTG) \
f(BC) \
f(BN) \
f(BNC) \
f(BNN) \
f(BNOV) \
f(BNZ) \
f(BOV) \
f(BRA) \
f(BZ) \
f(CALL) \
f(CLRWDT) \
f(DAW) \
f(GOTO) \
f(POP) \
f(PUSH) \
f(RCALL) \
f(RESET) \
f(RETFIE) \
f(RETLW) \
f(RETURN) \
f(SLEEP) \
f(ADDLW) \
f(ANDLW) \
f(IORLW) \
f(LFSRa) \
f(LFSRb) \
f(MOVLB) \
f(MOVLW) \
f(MULLW) \
f(SUBLW) \
f(XORLW) \
f(TBLRD) \
f(TBLRD_POSTINC) \
f(TBLRD_POSTDEC) \
f(TBLRD_PREINC) \
f(TBLWT) \
f(TBLWT_POSTINC) \
f(TBLWT_POSTDEC) \
f(TBLWT_PREINC) \
f(LABEL) \
typedef enum picOpcodeEnum {
#define PIC_OPCODES_ENUM(f) PIC_##f,
PIC_OPCODES(PIC_OPCODES_ENUM)
PIC_INST_CNT
} picOpcode;
//
// Special registers
//
enum {
PIC_SFR_START = 0xf80,
FSR0L = PIC_SFR_START,
FSR0H,
INDF0,
POSTINC0,
POSTDEC0,
PREINC0,
PLUSW0,
FSR1L,
FSR1H,
INDF1,
POSTINC1,
POSTDEC1,
PREINC1,
PLUSW1,
FSR2L,
FSR2H,
INDF2,
POSTINC2,
POSTDEC2,
PREINC2,
PLUSW2,
WREG,
STATUS,
BSR,
TRISA,
TRISB,
TRISC,
PORTA,
PORTB,
PORTC,
TBLPTRU,
TBLPTRH,
TBLPTRL,
TABLAT,
PRODH,
PRODL,
PIC_SFR_END
};
//
// Reset typesd
//
typedef enum picResetTypeEnum {
PIC_RESET_POWERON,
PIC_RESET_INSTRUCTION,
PIC_RESET_BROWNOUT,
PIC_RESET_WATCHDOG,
PIC_RESET_MCLR,
PIC_RESET_STACKOVER,
PIC_RESET_STACKUNDER,
PIC_RESET_CNT
} picResetType;
//###########################################################################
//############################### PROTOTYPES ################################
//###########################################################################
//
// create/destroy a pic chip
//
// name - name to identify this chip
// partnum - part number. e.g. pic18lf2525
//
picChip *picChipCreate(const char *name, const char *partnum);
void picChipDestroy(picChip *chip);
//
// run picsim test suite
//
void picTest(void);
//
// reset chip
//
void picChipReset(picChip *ch, enum picResetTypeEnum r);
//
// picTrace() - enable/disable tracing
// picGetCycle() - get cycle count
// picSetCycle() - set cycle count
//
int picTrace(int enable);
int picGetCycle(void);
void picSetCycle(int cyc);
//
// get/set register values (for debug)
//
// picReg*32 funcs are for up to 32 bits (use -1 in r2 & r3 for 16 bit)
// ValS is for signed
// ValU is for unsigned
// Set is signed or unsigned
//
unsigned int picRegX(picChip *chip, int regId);
unsigned int picRegValU(picChip *chip, int regId);
signed int picRegValS(picChip *chip, int regId);
void picRegSet(picChip *chip, int regId, int val);
unsigned int picRegX32(picChip *chip, int r0, int r1, int r2, int r3);
unsigned int picRegValU32(picChip *chip, int r0, int r1, int r2, int r3);
signed int picRegValS32(picChip *chip, int r0, int r1, int r2, int r3);
void picRegSet32(picChip *chip, int r0, int r1, int r2, int r3,
int val);
void picRegErase(picChip *chip, int regId);
void picRegEraseRange(picChip *chip, int regIdFirst, int regIdLast);
//
// Call this before running instructions
//
int picChipSetCurrent(picChip *chip);
//
// variable declaration
//
int picRegFind(picChip *chip, const char *name, const char *namespace);
//
// instruction (USE MACROS ABOVE)
//
int picInstDB(int reg, int dst, picOpcode op,const char *file, int line,
const char *dbstr);
//###########################################################################
//############################### GLOBALS ###################################
//###########################################################################
extern const char *picNamespaceGlobal;
extern const char *picNamespaceLocal;
extern const char *picNamespaceParam;
extern const char *picNamespaceInOutParam;
extern const char *picNamespaceReturn;
extern picChip *picCurChip;
|
This file Copyright (C) 2006 by Nathan (Acorn) Pooley
Go to TOP Wand page
Go to Acorn's personal webpage
Go to Hogwarts website: www.gotohogwarts.com
Snout: www.snout.org/game
Gadgets of Justice Unlimited
Snout GC (Wiki)
Snout Wiki
File created by do_doc at Wed May 30 03:30:19 PDT 2007