and a small aubergine...
22 May 2007
 
A lisp-based PIC assembler.

My latest project is an assembler for the PIC microcontroller. I figured it'd be an interesting project to embark on, and since I have a whole bunch of the microcontrollers lying around my place, it'd be something I could actually use.

One of the things that makes writing an assembler for the PIC interesting is that there are a few different instruction sets depending on the series you're using, the 18 series have 16 bit instructions and the 12/16 series have 14 bit instructions for example. Because of this, I wanted to make it so an instruction set was something that I could define easily:

(definstructionset pic16-instructions
    ((addwf        :args  (f d)
                   :description "Add W and f"
                   :opcode "00 0111 dfff ffff")
     (andwf        :args  (f d)
                   :description "AND W with f"
                   :opcode "00 0101 dfff ffff")
     (bsf          :args  (f b)
                   :description "Bit set f"
                   :opcode "01 01bb bfff ffff")
                   ...))

I decided to describe the opcodes as strings like that, as thats how they are defined in the official data sheets:

Eventually, I'd like to have a script that you could pass the datasheet PDF and it would auto generate the instruction set descriptor.

From that point a macro converts the instruction specs into a hash table of instruction symbols to instruction-spec classes:

(defclass instruction-spec ()
  ((mnemonic       :accessor spec-mnemonic
                   :initarg  :mnemonic)
   (description    :accessor spec-description
                   :initarg  :description)
   (opcode         :accessor spec-opcode
                   :initarg  :opcode)
   (base           :accessor spec-base       
                   :initarg  :base)
   (byte-specs     :accessor spec-byte-specs
                   :initarg  :byte-specs)
   (args           :accessor spec-args
                   :initarg  :args)))

The base slot of the class is an integer with all the parts of the opcode that are static (e.g the 0101 of the BSF instruction), and the byte-specs will be a list that maps instruction arguments (f, b, etc) to their respective parts of the opcode.

What's next? A way to tell the assembler about the different memory maps of PIC. This will be so that the assembler will automatically be aware of the all the PIC control registers without having to bring in / create an include file for each processor.

And from there the actual assembler/evaluator, right now I'm thinking that it'll be used like this:

(assemble   :target pic16f877
            :fuses  (NO_WATCHDOG NO_CODE_PROTECT)
    ((:start 0) ;; label with explicit start address
     (goto   setup)
     (:interrupt-service-routine 4)
     (goto   isr)

     (:setup
      (bsf    latb  2)
      (movlw  (* 6 7))
      (movwf  LIFE)

      (some-function-call some-arguments)
      ;; the output of the function called will be evaluated, like
      ;; macros.

      ...))) => #

;; and a way to turn the assembled output to into s-records ready for
;; a pic programmer.
Until next time... Edit: Fixed typos, remind me not to blog without sleep.
 
Comments:
This is one of the classic things that the ACL2 theorem prover gets used for. Here's a paper that might be interesting to you:

http://www.cs.utexas.edu/users/moore/publications/bm96.pdf
 
Eventually, I'd like to have a script that you could pass the datasheet PDF and it would auto generate the instruction set descriptor.

You know, I'd be interested in scraping PDFs for instruction sets, too. I was thinking about it, but it seems there'll be quite some manual effort needed still... Do you have concrete ideas about that?
 
Michael: not yet, it's just a pipe dream. Copying and pasting from the datasheet hasn't proven helpful either. It seems to copy one column a time! I'd like to be able to scrape the instruction set, the memory map and the capabilities (i.e, no# of timers + resolutions, PWM modules, etc.)
 
IKohm2 Your blog is great. Articles is interesting!
 
Q0BzZr Nice Article.
 
actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.
 
Magnific!
 
Good job!
 
Magnific!
 
Wonderful blog.
 
Good job!
 
Good job!
 
Please write anything else!
 
Yxl0Dk Magnific!
 
Hello all!
 
あなたの精神年齢を占ってみよう!当サイトは、みんなの「精神年齢度」をチェックする性格診断のサイトです。精神年齢度には、期待以上の意外な結果があるかも??興味がある方はぜひどうぞ
 
さあ、今夏も新たな出会いを経験してみませんか?当サイトは円助交際の逆、つまり女性が男性を円助する『逆円助交際』を提供します。逆円交際を未経験の方でも気軽に遊べる大人のマッチングシステムです。年齢上限・容姿・経験一切問いません。男性の方は無料で登録して頂けます。貴方も新たな出会いを経験してみませんか
 
みんなの精神年齢を測定できる、メンタル年齢チェッカーで秘められた年齢がズバリわかっちゃう!かわいいあの子も実は精神年齢オバサンということも…合コンや話のネタに一度チャレンジしてみよう
 
童貞卒業を考えているなら、迷わずココ!今まで童貞とヤッた事がない女性というのは意外と多いものです。そんな彼女たちは一度童貞とやってみたいと考えるのは自然な事と言えるでしょう。当サイトにはそんな好奇心旺盛な女性たちが登録されています
 
Post a Comment



<< Home

My Photo
Name: Jon Philpott
Location: Los Angeles, California, United States

Yet another code monkey from London, living in LA.

Links

  • Planet Lisp

    ARCHIVES
    June 2006 / July 2006 / August 2006 / September 2006 / November 2006 / May 2007 / June 2007 / August 2007 /


    Powered by Blogger