6 ms·
Interesting metrics, you'd expect C to do a lot better in the line-count department than assembly. I tried building it on Ubuntu, if you follow the instruction
by jacquesm 7y ago
Interesting metrics, you'd expect C to do a lot better in the line-count department than assembly.
I tried building it on Ubuntu, if you follow the instructions the SDL library include files will end up in a directory called SDL so you have to include SDL/SDL.h and even then the build fails with lots of SDL related definitions missing (SDL_Window for instance).
That's because you really should be doing
sudo apt-get install libsdl2-dev
Then change the include file line to
#include <SDL2/SDL.h>
and type:
make
The roms can be found here:
http://www.freevintagegames.com/MAME/invaders.php http://www.freevintagegames.com/MAME/invaders.php
After downloading you'll have to rename the files because the names will all be uppercase:
cd inv1
mv INVADERS.E invaders.e
mv INVADERS.F invaders.f
mv INVADERS.G invaders.g
mv INVADERS.H invaders.h
cd ..
Now the game should work:
./bin/si78c
Some minor nitpicks about the code:
- bracket your ifs and place the starting { on the same line as if/while, or one day you'll sit there staring at the screen for 8 hours trying to figure out why your code no longer works due to an accidentally deleted line.
So:
while (num < 64)
{
int has = SDL_PollEvent(&event_buffer[num]);
if (!has) break;
num++;
}
becomes:
while (num < 64) {
if (SDL_PollEvent(&event_buffer[num])) {
break;
}
num++;
}
Neat project!
- loadzero 7y agoThanks for pulling the code and trying it out, jacquesm! That kind of feedback is gold. I have updated the README with the correct Ubuntu package details. I think the issue with the includes is likely to do with having both SDL1 and 2 installed, and the slightly dirty way I am pulling in the header (so it works on Mac too). I will have a bit of a think about how best to resolve that issue, likely needs some ifdefing.
- pvg 7y agoyou'd expect C to do a lot better in the line-count department You would but it's worth keeping in mind this implementation is constrained by closely following the structure and logic of the original. It's like writing C but being told exactly what state you have to maintain (and where), down to the bit, in advance. It really is a 'hardware simulator' where the spec and input handed to you is the sequence of memory states (and a few other bits) of the original machine.
- loadzero 7y agoYes, I would say that those constraints are the main factor in not compacting the code further. The C version also loses a few lines here and there for function prototypes and structured control flow. Handwritten 8085 can be quite compact in some places due to sharing code fragments. It's a bit like self ROP.
- deleted 7y ago[deleted]
- Luc 7y ago> bracket your ifs and place the starting { on the same line as if/while, or one day you'll sit there staring at the screen for 8 hours trying to figure out why your code no longer works due to an accidentally deleted line. This is some rather strange advice. That and the rewrite dropped a '!'.
- pvg 7y agoWell, if you want to go all K&R you can just put the whole thing in a single while line, without the extra conditional inside. Single for line, if you're going for a more 'asshole C expert' style. If you're on your 200th hour trying to capture the flavour of the original 8085 assembly, you might end up writing something like what's in the code.
- jacquesm 7y agoThat's advice borne from a lifetime of programming C. Good catch about the !, but that was just as an example of the form, not meant as a cut-and-paste replacement.
- loadzero 7y agoBTW, I generally agree about extra indentation and braces for these kind of statements. The code is a bit terse due to a mechanical issue - my current laptop screen only gives me about 25 lines vertically.
- kazinator 7y agoI'm at 30 years of programming in C, and don't understand the comment. I've never had a problem with either curly brace placement. If a line is deleted, and that line is a curly brace, there will be a syntax error. "git diff" (or whatever you're using) will show you what lines you have deleted. Both ways have some merit. Cuddled curly is good for ismple one-liners. Curly on next line is when the statement header is stuffed with multi-line expressions. After all the years, I have settled on: while (simple) { } while ((big || complex) (condition && multiple && lines)) { } for (i = 0; i < MAX; i++) { } for (big_initialization; big && step; complex, increment++) { }
- bartread 7y agoIn C-like languages I generally prefer to see: int has = SDL_PollEvent(&event_buffer[num]); if (!has) ... to if (SDL_PollEvent(&event_buffer[num])) ... because it generally makes inspecting the return value of the function, in this case SDL_PollEvent, easier to inspect in a debugger. Particularly handy in the Visual Studio debugger when working with C#.
- speps 7y agoThe Autos tab in VS has the return values of functions after executing a line.
- NotCamelCase 7y agoHaving been using gdb (have to!) for a while again, one immediately comes to appreciate the wonder that is VS debugger.
- aidenn0 7y agoIt's possible to get the information with any debugger, you just need to know the ABI. On amd64 with the SystemV ABI, its %rax for integer return values and %xmm0 for floating-point return values. If you don't want to remember, you can write a macro for it. GDB probably has something builtin for this, but I gave up trying to find features in GDB a long time ago. [edit] Stackoverflow seems to be of the opinion that GDB doesn't have a tool for automatically showing the return value of a just-called function. However if you step into a function and execute the command "finish" you will return from the function and print the value. Properly showing the return value with source-level (rather than instruction-level) debugging is a non-trivial feature to get right.
- NotCamelCase 7y agoOf course, I can chase return registers by myself all day long, problem is I don't want to. Then again, what about pointers? PODs? strings? If you use VS debugger, you can see how distinctly it can display these, which is as different as night and day to me.
- AnIdiotOnTheNet 7y agoGood show dragging bracing style into the discussion. Nevermind that it is one of the oldest and least important battlegrounds in the history of programming, at least you got to be needlessly pedantic.
- naters 7y agoThat's where I assumed the comment was going as well, but it's actually not really about brace style at all. Rather it's about "if style" which is a much more interesting discussion (at least as applied to C error checking).
- jacquesm 7y agoThat's not a charitable reading of my comment, it actually does have practical impact and is not just about style but all about function. I could not care less about the looks but found out the hard way that separating the { from if and while constructs can bite you really hard.
- PavlovsCat 7y agoIt's the "deleting a random line without noticing it" part that bit you.
- aepiepaey 7y agoInstead of patching the source, the Makefile should be patched to use sdl2-config to set LDFLAGS and CFLAGS (instead of hard-coding them), i.e: CFLAGS := [other flags...] $(shell sdl2-config --cflags) LDFLAGS := [other flags...] $(shell sdl2-config --libs) That will set the correct compiler and linker flags for you installation, and you can leave the include as just "SDL.h".
- jacquesm 7y agoClever, I didn't know that sdl2-config was a thing.
- kazinator 7y agoIt may be clever, but short-sighted. It creates complexity for distro builds. sdl2-config has to be compiled for the host machine, and get installed in a build-time toolchain sysroot, and when invoked produce parameters for compiling for the target machine/sysroot. The correct sdl2-config built by the distro build has to be invoked; it must not be one that is locally installed in the build machine's /usr/bin. Imagine if every package with headers and libs had its' own "<package>-config" program! That would be a huge problem, which is why there is one program called package-config, to which packages or build systems just contribute blurbs of info in .pc files.
- jacquesm 7y agoAgreed, but still, it is something I didn't know about and I'm always impressed by the encyclopedic knowledge of HN about such arcane subjects. For similar reasons I listed a bunch of 'mv' commands because they will always work rather than the optional 'rename' package which would make it a one liner but less universal.
- vilda 7y agoIf you have rename installed, just run rename 'y/A-Z/a-z/' *
- kazinator 7y agoThere are several programs called rename with different argument conventions.
- kazinator 7y ago> Interesting metrics, you'd expect C to do a lot better in the line-count department than assembly. Not necessarily if the C code is trying to reproduce, in detail, all of the externally visible effects of the original assembly language.