
Avremu: An AVR Emulator Written in Pure LaTeX - dcschelt
https://gitlab.brokenpipe.de/stettberger/avremu/tree/master
======
jpallen
This is awesome, great job! How long before we can write ShareLaTeX in
ShareLaTeX? ;)

I once got as far as writing and http server that used the flexibility of TeX
macros to turn headers like 'content-length:' into TeX commands that recorded
their value, and then processed the incoming request as if it were TeX code.
It wasn't pretty...

------
tptacek
This is obviously impressive. AVR is surprisingly annoying to emulate! If
you're looking to emulate enough to run C on, two platforms that are
significantly less painful:

* MSP430, whose instruction set fits on a single Wikipedia page while simultaneously being nicer to program than AVR

* The c4 compiler's stack machine VM, which was designed specifically to be a minimal machine on which to run C in the smallest number of lines of code.

~~~
teiche
I'm not very familiar with the AVR instruction set, what makes it difficult to
emulate?

~~~
buserror
It's actually not terribly difficult. There are good references, and I
remember it took me about 4 days to write the very first version of simavr[0].

What's harder to emulate are all the peripherals, and all _their_ quirks.

In simavr the 'core' code is pretty much unchanged (apart from the odd
cosmetics) in the last 7 years, while most of the work was to emulate the
peripheral blocks.

[0]: [https://github.com/buserror/simavr](https://github.com/buserror/simavr)

~~~
tptacek
* It's got split I and D memory.

* All of I/O space is dual-addressed through both memory map and IO ports.

* Because there are insanely simple AVR parts that don't have a stack, the stack pointer is itself a peripheral.

* Half the register file is unusable for 16 bit instructions.

* The half that is usable includes 3 register pairs that have special pointer semantics.

* To accommodate pointer/array semantics in C, those three register pairs have an almost MOD/RM-ian collection of variant loads and stores, each specialized to a particular X, Y, or Z register.

* Because the stack pointer itself is a 16 bit IO peripheral, you have to get interrupt handling implemented properly to adjust it to support the dance AVR-GCC does to set it.

* The instruction encoding itself is a shotgun blast; read an AVR disassembler's mask and lookup table logic, for instance, which is something you don't even need to do in X86.

* For all that trouble, you don't even get a consistent instruction size.

I'd still rather do AVR than X86, but perhaps not by much.

------
dancsi
The obvious next step is to emulate ARM within it, and use it to run Linux:
[http://dmitry.gr/index.php?r=05.Projects&proj=07.%20Linux%20...](http://dmitry.gr/index.php?r=05.Projects&proj=07.%20Linux%20on%208bit)

~~~
i336_
Would be excellent for application performance testing.

------
krylon
It is amazing what crazy things people do just "because we can". I love that
simultaneous feeling of disgust, joy, horror and wonder. ;-)

------
i336_
This reminds me of
[https://github.com/uuner/sedtris](https://github.com/uuner/sedtris), a
console tetris game written in sed.

The code has a very similar visual feel to it:
[https://github.com/uuner/sedtris/blob/master/sedtris.sed](https://github.com/uuner/sedtris/blob/master/sedtris.sed)

------
dhosek
Well TeX is Turing-complete, so any computable program can be written and
executed using TeX. Back in the '80s, there was an MIT undergrad named Andrew
Marc Greene who wrote a BASIC interpretter in TeX
(BaSiX—[https://www.ctan.org/tex-
archive/macros/generic/basix](https://www.ctan.org/tex-
archive/macros/generic/basix)).

~~~
kahnjw
That's what the link says....

------
ezequiel-garzon
Could anyone point to the main (La)TeX files? I seem to find only
documentation such as [1].

[1]
[https://gitlab.brokenpipe.de/stettberger/avremu/blob/f634322...](https://gitlab.brokenpipe.de/stettberger/avremu/blob/f634322235a6b145535e1a28c2a2f33f91a01d64/source/avremu.tex)

~~~
btown
[https://gitlab.brokenpipe.de/stettberger/avremu/tree/master/...](https://gitlab.brokenpipe.de/stettberger/avremu/tree/master/tex/latex/avremu)

instr is a true work of art.

------
btown
But can it run Doom?

[https://motherboard.vice.com/read/a-catalogue-of-all-the-
dev...](https://motherboard.vice.com/read/a-catalogue-of-all-the-devices-that-
can-somehow-run-doom)

~~~
jacobush
It can run linux on an emaluated ARM, so probably yes

------
jdnier
There's also Stephen Hicks' winning 2008 ICFP programming contest entry, where
he writes "a controller for a Mars rover" in TeX.

[http://sdh33b.blogspot.dk/2008/07/icfp-
contest-2008.html](http://sdh33b.blogspot.dk/2008/07/icfp-contest-2008.html)

Rapid Prototyping in TeX: [https://wiki.haskell.org/wikiupload/8/85/TMR-
Issue13.pdf](https://wiki.haskell.org/wikiupload/8/85/TMR-Issue13.pdf)

------
wyc
Just when you thought you've finally grasped LaTeX...

------
godmodus
respect and awe. and a world of _hell no_ , for me personally.

it can churn 2.5khz & took it 44 hours to calculate a 250x250 mandelbrot set.

> This picture (250x250) took 44 hours to render. The source code can be found
> in the test-suite directory under mandelbrot.c.

here's the instruction code:
[https://gitlab.brokenpipe.de/stettberger/avremu/blob/master/...](https://gitlab.brokenpipe.de/stettberger/avremu/blob/master/tex/latex/avremu/avr.instr.tex)

pretty nice stuff. it looks simpler than it sounds, and it's probably MUCH
harder to properly implement than it looks.

------
szubster
I wonder whether it runs under
[http://manuels.github.io/texlive.js/](http://manuels.github.io/texlive.js/)

------
akavel
I like to say that TeX is the best example of an esoteric programming language
which sneaked into mainstream use.

------
Light2Yellow
Must have been so painful!

------
clifanatic
"Surely I'm misreading that title..."

"Nope, that's really what it is..."

~~~
dcschelt
My response exactly.

~~~
JohnHammersley
Mine too! Just when you thought you'd seen everything LaTeX could do (and we
see a lot of it at Overleaf[1]!)

If the author is reading these comments, what motivated/inspired you to do
this?

[1] [https://www.overleaf.com](https://www.overleaf.com)

------
shanth
Wut??? O_O

