Lurker News

Comments (105) 25 threads

Show all comments

Showing thread 4 of 25

jacquesm8 replies#
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
and type:

  make
The roms can be found here:

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#
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.

pvg1 reply#
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.

loadzero#
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.

Luc2 replies#
> 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#
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.
jacquesm2 replies#
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.
loadzero#
BTW, 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.

kazinator2 replies#
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++)
  {
  }

Continue this thread — 2 replies

bartread1 reply#
In 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#.

speps1 reply#
The Autos tab in VS has the return values of functions after executing a line.
NotCamelCase2 replies#
Having been using gdb (have to!) for a while again, one immediately comes to appreciate the wonder that is VS debugger.

Continue this thread — 2 replies

AnIdiotOnTheNet2 replies#
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.
naters#
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).
jacquesm#
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.
aepiepaey1 reply#
Instead 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".
jacquesm1 reply#
Clever, I didn't know that sdl2-config was a thing.
kazinator1 reply#
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.

Continue this thread — 1 reply

vilda1 reply#
If you have rename installed, just run

    rename 'y/A-Z/a-z/' *
kazinator#
There are several programs called rename with different argument conventions.
kazinator#
> 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.