Index: misc/fix-types.py
===================================================================
--- misc/fix-types.py	(revision bc11fc1450acb905f703961d148cb242b63fb6e0)
+++ misc/fix-types.py	(revision bc11fc1450acb905f703961d148cb242b63fb6e0)
@@ -0,0 +1,140 @@
+#!/usr/bin/env python3
+
+from sys import stdout
+from os import unlink
+from pycparser import c_ast, parse_file
+from re import escape, subn
+
+cross_gcc = "/opt/cross-m68k/bin/m68k-none-elf-gcc"
+
+class Visitor(c_ast.NodeVisitor):
+    def reset(self):
+        self.path = None
+        self.orig = None
+        self.subst = None
+
+    def __init__(self):
+        self.reset()
+
+    def visit_IdentifierType(self, node):
+        if node.coord.file != self.path or self.subst is not None:
+            return
+
+        new = None
+
+        if node.names == ["char"]:
+            new = "int8_t"
+
+        elif node.names == ["unsigned", "char"]:
+            new = "uint8_t"
+
+        elif node.names == ["short"] or \
+             node.names == ["int"]:
+            new = "int16_t"
+        elif node.names == ["unsigned", "short"] or \
+             node.names == ["unsigned", "int"] or \
+             node.names == ["unsigned"] or \
+             node.names == ["uint"] or \
+             node.names == ["UWORD16"]:
+            new = "uint16_t"
+
+        elif node.names == ["long"]:
+            new = "int32_t"
+
+        elif node.names == ["unsigned", "long"]:
+            new = "uint32_t"
+
+        elif node.names == ["void"] or \
+             node.names == ["int8_t"] or \
+             node.names == ["uint8_t"] or \
+             node.names == ["int16_t"] or \
+             node.names == ["uint16_t"] or \
+             node.names == ["int32_t"] or \
+             node.names == ["uint32_t"] or \
+             node.names == ["va_list"] or \
+             node.names == ["jmp_buf"] or \
+             node.names == ["BOOL"] or \
+             node.names == ["FILE"] or \
+             node.names == ["io_arg"] or \
+             node.names == ["LPF"]:
+            pass
+
+        else:
+            raise Exception("unknown type: {}".format(node.names))
+
+        if new is None:
+            self.generic_visit(node)
+            return
+
+        self.subst = (node.coord.line, node.names, new)
+
+    def get_ast(self):
+        with open(self.path, "w") as f:
+            f.write(self.orig)
+
+        ast = parse_file(self.path, use_cpp = True, cpp_path = cross_gcc,
+                         cpp_args = ["-E", "-I", "include", "-include", "predef.h"])
+        # ast.show()
+
+        unlink(self.path)
+        return ast
+
+    def fix(self, path, orig):
+        self.path = path
+        self.orig = orig
+
+        while True:
+            self.visit(self.get_ast())
+
+            if self.subst is None:
+                break
+
+            lines = self.orig.split("\n")
+            (line, old, new) = self.subst
+
+            beg = line - 3 if line > 3 else 0
+            end = line + 2 if line < len(lines) - 2 else len(lines)
+
+            focus = "\n".join(lines[beg : end])
+
+            old_re = "[\t ]".join([escape(x) for x in old]) + "([\t \)])"
+            (focus, n) = subn(old_re, new + "\g<1>", focus, 1)
+
+            if n != 1:
+                print(focus)
+                raise Exception("error while replacing {} with {} in {}:{}". \
+                                format(old_re, new, self.path, line))
+
+            self.orig = "\n".join(lines[: beg]) + \
+                        "\n" + focus + "\n" + \
+                        "\n".join(lines[end :])
+            self.subst = None
+
+        fixed = self.orig
+        self.reset()
+        return fixed
+
+vis = Visitor()
+
+with open("misc/c-files.txt", "r") as f:
+    for path in f:
+        path = path.rstrip()
+
+        if path == "ram/wdfield.c": # breaks pycparser
+            continue
+
+        stdout.write("fixing {}                    \r".format(path))
+        stdout.flush()
+
+        with open(path, "r") as f:
+            orig = f.read()
+
+        segs = path.split("/")
+        path_ = "/".join(segs[:-1] + ["_" + segs[-1]])
+
+        fixed = vis.fix(path_, orig)
+
+        with open(path, "w") as f:
+            f.write(fixed)
+
+    print("")
Index: misc/proto.c
===================================================================
--- misc/proto.c	(revision 60288f548947383de9a52a06e402df2698a3ce5d)
+++ misc/proto.c	(revision bc11fc1450acb905f703961d148cb242b63fb6e0)
@@ -37,5 +37,5 @@
 }
 
-void wdnfld(short k)
+void wdnfld(int16_t k)
 {
 }
@@ -51,13 +51,13 @@
 // ---------- ^^^ ---------- wdfield.c
 
-void clrvce(short vce)
-{
-}
-
-void execins(short vce, short ins, short tag)
-{
-}
-
-void execkey(short trg, short pch, short vce, short tag)
+void clrvce(int16_t vce)
+{
+}
+
+void execins(int16_t vce, int16_t ins, int16_t tag)
+{
+}
+
+void execkey(int16_t trg, int16_t pch, int16_t vce, int16_t tag)
 {
 }
@@ -71,5 +71,5 @@
 }
 
-unsigned fromfpu(unsigned fputime)
+uint16_t fromfpu(uint16_t fputime)
 {
     return 0;
@@ -80,5 +80,5 @@
 }
 
-void GLCplot(unsigned x, unsigned y, unsigned val)
+void GLCplot(uint16_t x, uint16_t y, uint16_t val)
 {
 }
@@ -92,5 +92,5 @@
 }
 
-void longjmp(struct JMP_BUF *env, int val)
+void longjmp(struct JMP_BUF *env, int16_t val)
 {
 }
@@ -100,13 +100,13 @@
 }
 
-void objclr(unsigned obj)
-{
-}
-
-void objoff(unsigned obj, unsigned line, unsigned num)
-{
-}
-
-void objon(unsigned obj, unsigned line, unsigned num)
+void objclr(uint16_t obj)
+{
+}
+
+void objoff(uint16_t obj, uint16_t line, uint16_t num)
+{
+}
+
+void objon(uint16_t obj, uint16_t line, uint16_t num)
 {
 }
@@ -116,13 +116,13 @@
 }
 
-void procpfl(unsigned trig)
-{
-}
-
-void _ptcl12(unsigned *fat, unsigned cl, unsigned val)
-{
-}
-
-long rand24(void)
+void procpfl(uint16_t trig)
+{
+}
+
+void _ptcl12(uint16_t *fat, uint16_t cl, uint16_t val)
+{
+}
+
+int32_t rand24(void)
 {
     return 0;
@@ -133,19 +133,19 @@
 }
 
-void se_disp(struct s_entry *ep, short sd, struct gdsel *gdstb[], short cf)
-{
-}
-
-struct s_entry *se_exec(struct s_entry *ep, short sd)
-{
-    return 0;
-}
-
-unsigned setipl(unsigned arg)
-{
-    return 0;
-}
-
-int setjmp(struct JMP_BUF *env)
+void se_disp(struct s_entry *ep, int16_t sd, struct gdsel *gdstb[], int16_t cf)
+{
+}
+
+struct s_entry *se_exec(struct s_entry *ep, int16_t sd)
+{
+    return 0;
+}
+
+uint16_t setipl(uint16_t arg)
+{
+    return 0;
+}
+
+int16_t setjmp(struct JMP_BUF *env)
 {
     return 0;
@@ -156,5 +156,5 @@
 }
 
-unsigned setsr(unsigned sr)
+uint16_t setsr(uint16_t sr)
 {
     return 0;
@@ -169,15 +169,15 @@
 }
 
-unsigned tofpu(unsigned time)
-{
-    return 0;
-}
-
-long trap13(short fun, ...)
-{
-    return 0;
-}
-
-long trap14(short fun, ...)
+uint16_t tofpu(uint16_t time)
+{
+    return 0;
+}
+
+int32_t trap13(int16_t fun, ...)
+{
+    return 0;
+}
+
+int32_t trap14(int16_t fun, ...)
 {
     return 0;
@@ -192,26 +192,26 @@
 }
 
-void tsplot4(int *obase, int nw, int fg, int row, int col, char *str, int pitch)
-{
-}
-
-long uldiv(long divid, long divis)
-{
-    return 0;
-}
-
-void vbank(unsigned b)
-{
-}
-
-void vclrav(unsigned *adr, unsigned row, unsigned col, unsigned atr, unsigned len)
-{
-}
-
-void vcputs(int *obase, int nw, int fg, int bg, int row, int col, char *str)
-{
-}
-
-void vcputsv(int *obase, int nw, int fg, int bg, int row, int col, char *str, int pitch)
+void tsplot4(int16_t *obase, int16_t nw, int16_t fg, int16_t row, int16_t col, int8_t *str, int16_t pitch)
+{
+}
+
+int32_t uldiv(int32_t divid, int32_t divis)
+{
+    return 0;
+}
+
+void vbank(uint16_t b)
+{
+}
+
+void vclrav(uint16_t *adr, uint16_t row, uint16_t col, uint16_t atr, uint16_t len)
+{
+}
+
+void vcputs(int16_t *obase, int16_t nw, int16_t fg, int16_t bg, int16_t row, int16_t col, int8_t *str)
+{
+}
+
+void vcputsv(int16_t *obase, int16_t nw, int16_t fg, int16_t bg, int16_t row, int16_t col, int8_t *str, int16_t pitch)
 {
 }
@@ -225,43 +225,43 @@
 }
 
-void vputa(unsigned *sbase, unsigned row, unsigned col, unsigned attrib)
-{
-}
-
-void vputc(unsigned *sbase, unsigned row, unsigned col, unsigned c, unsigned attrib)
-{
-}
-
-void vputcv(unsigned *adr, unsigned row, unsigned col, unsigned char, unsigned atr, unsigned cols)
-{
-}
-
-void vputp(struct octent *octad, int xloc, int yloc, int val)
-{
-}
-
-void vsetav(unsigned *adr, unsigned row, unsigned col, unsigned atr, unsigned len)
-{
-}
-
-void vsetcv(unsigned *adr, unsigned row, unsigned col, unsigned cfb, unsigned len)
-{
-}
-
-void vsplot4(unsigned *obase, unsigned nw, unsigned fg, unsigned row, unsigned col, char *str,
-             unsigned pitch, unsigned ht, int cgtab[][256])
-{
-}
-
-void vvputsv(unsigned *obase, unsigned nw, unsigned fg, unsigned bg, unsigned row, unsigned col,
-             char *str, unsigned pitch, unsigned ht, int cgtab[][256])
-{
-}
-
-void vwputp(struct octent *octad, int xloc, int yloc, int val)
-{
-}
-
-void vwputs(int *obase, int nw, int fg, int bg, int row, int col, char *str)
+void vputa(uint16_t *sbase, uint16_t row, uint16_t col, uint16_t attrib)
+{
+}
+
+void vputc(uint16_t *sbase, uint16_t row, uint16_t col, uint16_t c, uint16_t attrib)
+{
+}
+
+void vputcv(uint16_t *adr, uint16_t row, uint16_t col, uint8_t chr, uint16_t atr, uint16_t cols)
+{
+}
+
+void vputp(struct octent *octad, int16_t xloc, int16_t yloc, int16_t val)
+{
+}
+
+void vsetav(uint16_t *adr, uint16_t row, uint16_t col, uint16_t atr, uint16_t len)
+{
+}
+
+void vsetcv(uint16_t *adr, uint16_t row, uint16_t col, uint16_t cfb, uint16_t len)
+{
+}
+
+void vsplot4(uint16_t *obase, uint16_t nw, uint16_t fg, uint16_t row, uint16_t col, int8_t *str,
+             uint16_t pitch, uint16_t ht, int16_t cgtab[][256])
+{
+}
+
+void vvputsv(uint16_t *obase, uint16_t nw, uint16_t fg, uint16_t bg, uint16_t row, uint16_t col,
+             int8_t *str, uint16_t pitch, uint16_t ht, int16_t cgtab[][256])
+{
+}
+
+void vwputp(struct octent *octad, int16_t xloc, int16_t yloc, int16_t val)
+{
+}
+
+void vwputs(int16_t *obase, int16_t nw, int16_t fg, int16_t bg, int16_t row, int16_t col, int8_t *str)
 {
 }
