Live data from Hacker News

CHIP-8 in Common Lisp

stevelosh.com

1–10 of 21 posts

Re: CHIP-8 in Common Lisp

#2
> Also note that the program counter starts at address #x200, because that’s where the ROM data eventually gets loaded into the CHIP-8 memory.

If I understand correctly, that's because 0-1FF was ROM (including the interpreter itself) on the original machine, and you typed CHIP-8 programs into RAM.

Re: CHIP-8 in Common Lisp

#3
Very interesting read! But could somebody explain why the following piece of code is needed? And why is it defined inline?

  (defun-inline (setf vref) (new-value chip x y)
     (setf (aref (chip-video chip) (+ (* +screen-width+ y) x))
        new-value))

Re: CHIP-8 in Common Lisp

#4
post #3

Very interesting read! But could somebody explain why the following piece of code is needed? And why is it defined inline? (defun-inline (setf vref) (new-value chip x y) (setf (aref (chip-video chip) (+ (* +screen-width+ y) x)) new-value))

vref and (setf vref) define getter and setter functions for manipulating pixel arrays. Since these functions are used a lot, it makes sense to allow compiler to inline them for maximum speed.

Normally function must be accessed trough the symbol and it can be redefined on the fly.

Re: CHIP-8 in Common Lisp

#5
post #3

Very interesting read! But could somebody explain why the following piece of code is needed? And why is it defined inline? (defun-inline (setf vref) (new-value chip x y) (setf (aref (chip-video chip) (+ (* +screen-width+ y) x)) new-value))

vref and (setf vref) define getter and setter functions for manipulating pixel arrays. Since these functions are used a lot, it makes sense to allow compiler to inline them for maximum speed. Normally function must be accessed trough the symbol and it can be redefined on the fly.

It would be interesting to do a measurement of inline vs non-inline to see how much of a speed difference it makes.

Re: CHIP-8 in Common Lisp

#6
post #3

Very interesting read! But could somebody explain why the following piece of code is needed? And why is it defined inline? (defun-inline (setf vref) (new-value chip x y) (setf (aref (chip-video chip) (+ (* +screen-width+ y) x)) new-value))

vref and (setf vref) define getter and setter functions for manipulating pixel arrays. Since these functions are used a lot, it makes sense to allow compiler to inline them for maximum speed. Normally function must be accessed trough the symbol and it can be redefined on the fly.

Functions don't need to be accessed through them symbol. A file compiler can assume that a function does not change inside a file, etc. Inlining and being called via a symbol are slightly different things...

I would also think that lexical functions are not called via symbols...

Re: CHIP-8 in Common Lisp

#7
post #3

Very interesting read! But could somebody explain why the following piece of code is needed? And why is it defined inline? (defun-inline (setf vref) (new-value chip x y) (setf (aref (chip-video chip) (+ (* +screen-width+ y) x)) new-value))

Why is DEFUN-INLINE used over:

    (declaim (inline (setf vref)))
    (defun (setf vref) (new-value chip x y)
       (setf (aref (chip-video chip) (+ (* +screen-width+ y) x))
          new-value))

Re: CHIP-8 in Common Lisp

#8

> Also note that the program counter starts at address #x200, because that’s where the ROM data eventually gets loaded into the CHIP-8 memory. If I understand correctly, that's because 0-1FF was ROM (including the interpreter itself) on the original machine, and you typed CHIP-8 programs into RAM.

Yes, traditionally the interpreter was located there. However, there is no "original machine". CHIP-8 was originally designed as a virtual machine for portability, similar to Java.

https://en.wikipedia.org/wiki/CHIP-8

Re: CHIP-8 in Common Lisp

#9
post #7
post #3

Very interesting read! But could somebody explain why the following piece of code is needed? And why is it defined inline? (defun-inline (setf vref) (new-value chip x y) (setf (aref (chip-video chip) (+ (* +screen-width+ y) x)) new-value))

Why is DEFUN-INLINE used over: (declaim (inline (setf vref))) (defun (setf vref) (new-value chip x y) (setf (aref (chip-video chip) (+ (* +screen-width+ y) x)) new-value))

Less typing/reading.

Re: CHIP-8 in Common Lisp

#10

> Also note that the program counter starts at address #x200, because that’s where the ROM data eventually gets loaded into the CHIP-8 memory. If I understand correctly, that's because 0-1FF was ROM (including the interpreter itself) on the original machine, and you typed CHIP-8 programs into RAM.

Yes, traditionally the interpreter was located there. However, there is no "original machine". CHIP-8 was originally designed as a virtual machine for portability, similar to Java. https://en.wikipedia.org/wiki/CHIP-8

Yes; I mean the machines on which the original interpreter ran, not that CHIP-8 itself was originally released in a hardware implementation.
Post reply on HN