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
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++;
}
Thanks 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.
you'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.
Yes, 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.
> 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 '!'.
Well, 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.
That'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.
I'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++)
{
}
Good 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.
That'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).
That'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.
It 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 "-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.
Comments (105) 25 threads
Show all comments
Showing thread 4 of 25
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).
Then change the include file line to and type: The roms can be found here:That's because you really should be doing
http://www.freevintagegames.com/MAME/invaders.php
Now the game should work: Some minor nitpicks about the code:After downloading you'll have to rename the files because the names will all be uppercase:
- 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.
becomes: Neat project!So:
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.
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.
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.
This is some rather strange advice. That and the rewrite dropped a '!'.
The code is a bit terse due to a mechanical issue - my current laptop screen only gives me about 25 lines vertically.
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:
Continue this thread — 2 replies
Particularly handy in the Visual Studio debugger when working with C#.
Continue this thread — 2 replies
Imagine if every package with headers and libs had its' own "-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.
Continue this thread — 1 reply
Not necessarily if the C code is trying to reproduce, in detail, all of the externally visible effects of the original assembly language.