On 09/14/2013 12:14 PM, Tomás Coiro wrote:
I started to think about programming a Chip16 emulator
(https://github.com/tykel/chip16).
Mainly to practice having a somewhat big project in Racket and hopefully
learning something about how basic hardware behaves.

Before even starting to program I couldn't decide how i would simulate
signed bytes.
Now, I realize these options are possible to program, and maybe not even
hard, but I'd like some advice.

Should I use integers? bitwise-and and such allow for some easy use
there, but then I'd need some special functions to sum, multiply,
subtract, etc.

Byte-strings are another option, but they don't support either and, or,
addition, or subtraction, so I would have to make functions for all of
those.

And then I though of vectors of 1's and 0's, which would also carry a
load of functions to define.

Is there any way I can simulate signed bytes behavior without
implementing a lot of functions? Is there an alternative that i haven't
though of?

Unless you want to write an FFI, I think you'll end up writing the functions yourself. Arithmetic functions will have to either return the flags register or destructively update it. If you want the flags for free, you'll have to write your code in a low-level language (like assembly, or C with the right libraries) that gives you access to the x86 FLAGS register. That assumes Chip16's flags are set under the same conditions, of course.

I'd start with something like this:


#lang typed/racket

(require racket/fixnum)

(: sbyte->fixnum (Byte -> Fixnum))
(define (sbyte->fixnum b)
  (if (b . > . 127) (- b 256) b))

(: fixnum->sbyte (Fixnum -> Byte))
(define (fixnum->sbyte s)
  (cast (modulo s 256) Byte))

(: sbyte+ (Byte Byte -> Byte))
(define (sbyte+ s1 s2)
  (fixnum->sbyte (fx+ (sbyte->fixnum s1) (sbyte->fixnum s2))))

(: sbyte- (Byte Byte -> Byte))
(define (sbyte- s1 s2)
  (fixnum->sbyte (fx- (sbyte->fixnum s1) (sbyte->fixnum s2))))


Then I'd add the flags register as either an arithmetic function argument and return value, or a global value.

Neil ⊥

____________________
 Racket Users list:
 http://lists.racket-lang.org/users

Reply via email to