• AVR Freaks

Hot!New PIC Emulator written in C (By me)

Author
MJaoune
Junior Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2019/06/04 07:08:56
  • Location: 0
  • Status: offline
2019/06/04 15:46:28 (permalink)
5 (6)

New PIC Emulator written in C (By me)

I have chosen for my Bachelor Senior Project to write a PIC16 Emulator (And assembler) from scratch using C, and it was successful after 8 months of constant work; it can run almost any basic PIC16 assembly code written for P16F84A (The only emulated model at the moment).

I am releasing the project as an open-source project to the public for people to benefit from and contribute to.

Project's GIT repository: https://codeberg.org/MJaoune/pic-emulator

It doesn't need any dependencies, just build it using "make" and you are good to go. You can test one of the PIC16 assembly source files located in "test_files/" directory, or test your own PIC16 program (More info in README.md).

I have written the project with scalability in mind, it has the ability to support other PIC architectures than just PIC16, and also adding support to other PIC16 models is fairly easy for any good C programmer with emulation experience (Just a matter of copy-paste).

Features:
- Written in C from scratch without any dependencies.
- Currently supports 3 directives: EQU, ORG and #include.
- Supports macros.
- Currently compiles PIC16 assembly code and emulates P16F84A.
- Can support other PIC models and architectures.
- Emulates timers and EEPROM.
- Emulates interrupts.
- It is open-source.
- Lightweight (Good alternative if you don't like MPLAB for being bulky).
- Cross-platform (Compiles on any OS for any OS).

P.S. You might find some bugs when using the program since it is still in Alpha, please report them in the GIT repository on CodeBerg, or perhaps fix them and create a Pull Request (That would be appreciated).
#1

10 Replies Related Threads

    PStechPaul
    Super Member
    • Total Posts : 2431
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: New PIC Emulator written in C (By me) 2019/06/05 18:01:41 (permalink)
    0
    Looks interesting. What would you recommend to use this on Windows 10? I found the following:
     
    http://www.codebind.com/cprogramming/install-mingw-windows-10-gcc/
     
    I also have dual boot Ubuntu Linux, but I prefer using Win 10.

     
    #2
    MJaoune
    Junior Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2019/06/04 07:08:56
    • Location: 0
    • Status: offline
    Re: New PIC Emulator written in C (By me) 2019/06/05 19:45:05 (permalink)
    0
    PStechPaul
    Looks interesting. What would you recommend to use this on Windows 10? I found the following:
     
    http://www.codebind.com/cprogramming/install-mingw-windows-10-gcc/
     
    I also have dual boot Ubuntu Linux, but I prefer using Win 10.




    On Windows I would prefer using one of the POSIX environments such as MSYS2 or Cygwin. I have built the code for Windows under MSYS2 and you may download the binary release from here: https://drive.google.com/open?id=198W0p2qCYbUBD9jDRMhPEdpBklS31goU
     
    Codeberg seems to have a problem with uploading ZIP files that is why I uploaded it on Google Drive.
    #3
    PStechPaul
    Super Member
    • Total Posts : 2431
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: New PIC Emulator written in C (By me) 2019/06/05 20:23:44 (permalink)
    0
    I ran the program using a batch file and here is the output:
    No filename passed for source code... Assuming 'test . asm'
    Compiler filename: test . asm
    Preproccessing source file...
    Compiling...

    Compilation done. Program size 20

    Program memory:
    -----------
    30 01 00 8D 30 02 00 8E 30 03 00 8F 30 04 00 90 30 05 00 91
    -----------

    Initializing PIC of model P16F84A.

    Installing program

    Emulation started...

    Cycle 1
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    E X E C (pc=0): 12289
    Instruction: MOVLW
    Processing timer
    =========================
    Work: 0x01

    RAM:

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    00: 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00
    01: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    02: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    03: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    04: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    05: 00 FF 02 00 00 1F FF 00 00 00 00 00 00 00 00 00
    06: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    07: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    09: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    =========================

    Timer 0: 0.00
    ==================

    Cycle 2
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    E X E C (pc=2): 141
    Instruction: MOVWF
    Bank: 0 | Type: 12 | Data: 1
    Processing timer
    =========================
    Work: 0x01

    RAM:

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    00: 00 00 04 00 00 00 00 00 00 00 00 00 00 01 00 00
    01: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    02: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    03: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    04: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    05: 00 FF 04 00 00 1F FF 00 00 00 00 00 00 01 00 00
    06: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    07: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    09: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    =========================

    Timer 0: 0.00
    ==================

    .....

    Cycle 11
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    Ex ecution ended.


     
    I don't know how this might be useful for me, but it's an interesting project. Thanks.

     
    #4
    PStechPaul
    Super Member
    • Total Posts : 2431
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: New PIC Emulator written in C (By me) 2019/06/05 20:28:37 (permalink)
    0
    Apparently the firewall doesn't like the word "E X E C". Here is the test.asm file:
    ;Mahmoud Jaoune
    ;Basic memory transfer Example
    movlw 1
    movwf 0xD

    movlw 2
    movwf 0xE

    movlw 3
    movwf 0xF

    movlw 4
    movwf 0x10

    movlw 5
    movwf 0x11


     
    And the output (modified to appease the firewall daemons):
    No filename passed for source code... Assuming 'test.asm'
    Compiler filename: test.asm
    Preproccessing source file...
    Compiling...

    Compilation done. Program size 20

    Program memory:
    -----------
    30 01 00 8D 30 02 00 8E 30 03 00 8F 30 04 00 90 30 05 00 91
    -----------

    Initializing PIC of model P16F84A.

    Installing program

    Emulation started...

    Cycle 1
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    EX EC (pc=0): 12289
    Instruction: MOVLW
    Processing timer
    =========================
    Work: 0x01

    RAM:

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    00: 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00
    01: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    02: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    03: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    04: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    05: 00 FF 02 00 00 1F FF 00 00 00 00 00 00 00 00 00
    06: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    07: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    09: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    =========================

    Timer 0: 0.00
    ==================

    Cycle 2
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    EX EC (pc=2): 141
    Instruction: MOVWF
    Bank: 0 | Type: 12 | Data: 1
    Processing timer
    =========================
    Work: 0x01

    RAM:

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    00: 00 00 04 00 00 00 00 00 00 00 00 00 00 01 00 00
    01: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    02: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    03: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    04: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    05: 00 FF 04 00 00 1F FF 00 00 00 00 00 00 01 00 00
    06: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    07: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    09: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    =========================

    Timer 0: 0.00
    ==================

    Cycle 3
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    EX EC (pc=4): 12290
    Instruction: MOVLW
    Processing timer
    =========================
    Work: 0x02

    RAM:

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    00: 00 00 06 00 00 00 00 00 00 00 00 00 00 01 00 00
    01: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    02: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    03: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    04: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    05: 00 FF 06 00 00 1F FF 00 00 00 00 00 00 01 00 00
    06: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    07: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    09: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    =========================

    Timer 0: 0.00
    ==================

    Cycle 4
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    EX EC (pc=6): 142
    Instruction: MOVWF
    Bank: 0 | Type: 12 | Data: 2
    Processing timer
    =========================
    Work: 0x02

    RAM:

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    00: 00 00 08 00 00 00 00 00 00 00 00 00 00 01 02 00
    01: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    02: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    03: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    04: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    05: 00 FF 08 00 00 1F FF 00 00 00 00 00 00 01 02 00
    06: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    07: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    09: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    =========================

    Timer 0: 0.00
    ==================

    Cycle 5
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    EX EC (pc=8): 12291
    Instruction: MOVLW
    Processing timer
    =========================
    Work: 0x03

    RAM:

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    00: 00 00 0A 00 00 00 00 00 00 00 00 00 00 01 02 00
    01: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    02: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    03: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    04: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    05: 00 FF 0A 00 00 1F FF 00 00 00 00 00 00 01 02 00
    06: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    07: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    09: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    =========================

    Timer 0: 0.00
    ==================

    Cycle 6
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    EX EC (pc=10): 143
    Instruction: MOVWF
    Bank: 0 | Type: 12 | Data: 3
    Processing timer
    =========================
    Work: 0x03

    RAM:

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    00: 00 00 0C 00 00 00 00 00 00 00 00 00 00 01 02 03
    01: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    02: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    03: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    04: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    05: 00 FF 0C 00 00 1F FF 00 00 00 00 00 00 01 02 03
    06: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    07: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    09: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    =========================

    Timer 0: 0.00
    ==================

    Cycle 7
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    EX EC (pc=12): 12292
    Instruction: MOVLW
    Processing timer
    =========================
    Work: 0x04

    RAM:

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    00: 00 00 0E 00 00 00 00 00 00 00 00 00 00 01 02 03
    01: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    02: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    03: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    04: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    05: 00 FF 0E 00 00 1F FF 00 00 00 00 00 00 01 02 03
    06: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    07: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    09: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    =========================

    Timer 0: 0.00
    ==================

    Cycle 8
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    EX EC (pc=14): 144
    Instruction: MOVWF
    Bank: 0 | Type: 12 | Data: 4
    Processing timer
    =========================
    Work: 0x04

    RAM:

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    00: 00 00 10 00 00 00 00 00 00 00 00 00 00 01 02 03
    01: 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    02: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    03: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    04: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    05: 00 FF 10 00 00 1F FF 00 00 00 00 00 00 01 02 03
    06: 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    07: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    09: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    =========================

    Timer 0: 0.00
    ==================

    Cycle 9
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    EX EC (pc=16): 12293
    Instruction: MOVLW
    Processing timer
    =========================
    Work: 0x05

    RAM:

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    00: 00 00 12 00 00 00 00 00 00 00 00 00 00 01 02 03
    01: 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    02: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    03: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    04: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    05: 00 FF 12 00 00 1F FF 00 00 00 00 00 00 01 02 03
    06: 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    07: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    09: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    =========================

    Timer 0: 0.00
    ==================

    Cycle 10
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    EX EC (pc=18): 145
    Instruction: MOVWF
    Bank: 0 | Type: 12 | Data: 5
    Processing timer
    =========================
    Work: 0x05

    RAM:

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    00: 00 00 14 00 00 00 00 00 00 00 00 00 00 01 02 03
    01: 04 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    02: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    03: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    04: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    05: 00 FF 14 00 00 1F FF 00 00 00 00 00 00 01 02 03
    06: 04 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    07: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    08: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    09: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    =========================

    Timer 0: 0.00
    ==================

    Cycle 11
    ==================
    Processing interrupts
    Processing EEPROM
    Processing instruction
    Bank: 0
    Ex ecution ended.



     
    #5
    MJaoune
    Junior Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2019/06/04 07:08:56
    • Location: 0
    • Status: offline
    Re: New PIC Emulator written in C (By me) 2019/06/05 20:28:54 (permalink)
    0
    Currently the emulator shows the execution of each cycle while printing memory content and work register content.
    post edited by MJaoune - 2019/06/05 20:30:24
    #6
    PStechPaul
    Super Member
    • Total Posts : 2431
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: New PIC Emulator written in C (By me) 2019/06/05 20:53:39 (permalink)
    5 (1)
    It emulated the "test.asm" file that I had in the same directory. I also tried it for "test2.asm".
     
    Perhaps the basic emulation engine could be adapted to a GUI application and enhanced to provide some of the functionality of the MPLABX simulator. If nothing else this illustrates the amount of effort needed for emulation.

     
    #7
    MJaoune
    Junior Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2019/06/04 07:08:56
    • Location: 0
    • Status: offline
    Re: New PIC Emulator written in C (By me) 2019/06/06 02:32:48 (permalink)
    0
    PStechPaul
    It emulated the "test.asm" file that I had in the same directory. I also tried it for "test2.asm".
     
    Perhaps the basic emulation engine could be adapted to a GUI application and enhanced to provide some of the functionality of the MPLABX simulator. If nothing else this illustrates the amount of effort needed for emulation.




    The graduation project itself had a GUI which this emulator would communicate with through TCP, but that GUI (IDE) wasn't really professionally designed so I chose to remove it prior to releasing the project to the public.
     
    Perhaps if I see more attention given to my project I wouldn't mind improving it (Adding more archs, models, etc...).
    post edited by MJaoune - 2019/06/06 02:34:03
    #8
    MJaoune
    Junior Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2019/06/04 07:08:56
    • Location: 0
    • Status: offline
    Re: New PIC Emulator written in C (By me) 2019/06/18 02:27:11 (permalink)
    4.5 (2)
    v0.2 is now out in Releases tab in Codeberg. Windows users may download the win32-msys2 build.
    #9
    InvalidApple
    Super Member
    • Total Posts : 296
    • Reward points : 0
    • Joined: 2011/05/17 23:36:35
    • Location: Melbourne, Australia
    • Status: offline
    Re: New PIC Emulator written in C (By me) 2019/06/20 00:16:02 (permalink)
    0
    (deleted)

    I misread the code..
    post edited by InvalidApple - 2019/06/20 00:20:53
    #10
    MJaoune
    Junior Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2019/06/04 07:08:56
    • Location: 0
    • Status: offline
    Re: New PIC Emulator written in C (By me) 2019/09/11 04:38:49 (permalink)
    5 (1)
    Some people might be having difficulties building the project from source due to autotools missing some "installation files". Just execute "autoreconf -fi" prior to executing "./configure" and "make". This will setup your working directory and generate the configure script.
    #11
    Jump to:
    © 2019 APG vNext Commercial Version 4.5