Skip to content

Instantly share code, notes, and snippets.

View durka's full-sized avatar

Alex Burka durka

View GitHub Profile
/* the memory is in an array Mem, which is of type int[] or char[] or something */
Mem[B] -= Mem[A];
if (Mem[B] <= 0) goto C;
else goto (IP + 3);
if (A < 0) A = Mem[-A]; /* treat negative numbers as pointers-to-pointers */
if (B < 0) B = Mem[-B];
if (C < 0) C = Mem[-C];
Mem[B] -= Mem[A]; /* as before, using the maybe-adjusted A, B, and C */
if (Mem[B] <= 0) goto C;
else goto (IP + 3);
(defn subleq
([in out mem regs] ; in this case data and program memory are the same
(apply subleq in out mem regs (subvec mem (:ip regs) (+ 3 (:ip regs)))))
([in out mem regs a b c]
(let [a-ind (if (neg? a) (nth mem (- a)) a)
b-ind (if (neg? b) (nth mem (- b)) b)
c-ind (if (neg? c) (nth mem (- c)) c)
mem (assoc mem b-ind
(- (mem b-ind) (mem a-ind)))]
;; First example:
Clojure=> (subleq 3 4 [5 8 12 0 0 42 0 0 144 1 2 3 4 5 6 7] {:ip 0}) ; here x = 42 and y = 144
[[5 8 12 0 0 42 0 0 102 1 2 3 4 5 6 7] {:out false, :in false, :ip 3}]
; Mem[A] has been subtracted from Mem[B], but Mem[B] is 102 so we do not jump to C
Clojure=> (subleq 3 4 [5 8 12 0 0 144 0 0 42 1 2 3 4 5 6 7] {:ip 0}) ; now switch Mem[A] and Mem[B]
[[5 8 12 0 0 144 0 0 -102 1 2 3 4 5 6 7] {:out false, :in false, :ip 12}]
;; Second example, with a negative operand:
Clojure=> (subleq 3 4 [5 8 -12 0 0 42 0 0 144 1 2 3 14 5 6 7] {:ip 0})
[[5 8 -12 0 0 42 0 0 102 1 2 3 14 5 6 7] {:out false, :in false, :ip 3}]
(defn drive-subleq
"Runs an OISC SUBLEQ program. Given the program (as a list of integers) and the input (in the same way), it runs the program and returns the output."
[program input]
(let [-out- (count program)
-in- (inc -out-)
input-len (count input)]
(loop [mem (vec (concat program
[0 (if (zero? input-len) -1 (nth input 0))] ; fill in the first char of the input now, unless there is no input, in which case give EOF right away
(vec (range 0 1023)))) ; memory starts out as the program, then the special output cell, then the special input cell, followed by 1024 memory cells filled with the numbers from 0 to 1023
regs {:ip 0}
Clojure=> (drive-subleq [85 20 3, 20 18 6, 20 20 9, 19 20 12, 20 79 15, 79 19 18] "")
["A" 58] ; cool!
@durka
durka / gist:3910107
Created October 18, 2012 05:57
Latex/programming keyboard for KeyRemap4Macbook
<item>
<name>LaTeX Keyboard</name>
<appendix>With Control_L as a modifier, overlay all the alphabetic keys with characters useful for LaTeX (and programming in general).</appendix>
<identifier>private.latex_keyboard</identifier>
<!-- the basic trick: CONTROL_L+key => CONTROL_R+key, but CONTROL_L => ESCAPE -->
<autogen>--KeyOverlaidModifier-- KeyCode::CONTROL_L, KeyCode::CONTROL_R, KeyCode::ESCAPE</autogen>
<autogen>--KeyToKey-- KeyCode::OPTION_L, KeyCode::CONTROL_L</autogen>
<!-- keys from http://www.autohotkey.com/community/viewtopic.php?t=18536 -->
@durka
durka / 1. mspdebug output
Created October 31, 2012 02:48
Failure to program Launchpad
alex$ make
Generating dependencies hello_world.d from hello_world.c
Compiling hello_world.c
Linking hello_world.elf
>>>> Size of Firmware <<<<
text data bss dec hex filename
316 50 0 366 16e hello_world.elf
unix2dos: converting file hello_world.txt to DOS format ...
require 'formula'
class Mspdebug < Formula
homepage 'http://mspdebug.sourceforge.net/'
url 'http://sourceforge.net/projects/mspdebug/files/mspdebug-0.20.tar.gz'
sha1 'ddf589e6e15c2577fc132001f757113309e134e9'
head 'git://mspdebug.git.sourceforge.net/gitroot/mspdebug/mspdebug'
depends_on 'libusb-compat'
function t = T(params)
d = length(params);
t = eye(d+1);
if isa(params, 'sym')
t = sym(t);
end
t(1:d,d+1) = params;
end