Errors converting from C (mostly "expected unqualified-id before...")

I'm converting a 6502 emulator I wrote in C a couple years ago to work on my Arduino Uno. It's pretty small, I think this will work. It's pretty straightforward C code, nothing fancy. However, I'm having some errors. Here's what I get when trying to compile the sketch... it looks like something is getting broken by pre-processing.

_6502_c:89: error: expected unqualified-id before '&&' token
_6502_c:94: error: expected unqualified-id before numeric constant
_6502_c:94: error: expected `)' before numeric constant
_6502_c:103: error: expected unqualified-id before 'asm'
_6502_c:134: error: expected unqualified-id before 'asm'
_6502_c:253: error: expected unqualified-id before '&&' token

The code is a little large to post on here, I think... 875 lines. Here it is on pastebin:

http://pastebin.com/USg72mH9

I haven't fooled around with the Arduino that much, so I'm probably just doing something obviously wrong. If anybody can help, it would be much appreciated. Thanks! :)

You can add attachments.

How to use this forum

The Auto Format tool in the IDE reports that there are too many right curly braces in your code. Until that is fixed it will not compile.

This is an incredibly interesting project, of course. I love the way the internal state machine has been reverse engineered.

http://www.pagetable.com/?p=517

And check this out:

http://www.weihenstephan.org/~michaste/pagetable/6502/6502.jpg

And the schematic for the chip:

http://www.downloads.reactivemicro.com/Public/Electronics/CPU/6502%20Schematic.pdf

UKHeliBob: The Auto Format tool in the IDE reports that there are too many right curly braces in your code. Until that is fixed it will not compile.

It didn't like my multi-line macro defines for the flag calculation stuff. However, I still get the same errors. I think the auto-format problems aren't affecting the compile. The formatter doesn't like them, but the compiler itself doesn't care. (Possible auto-formatter bug?)

This same code compiles file in Visual C++ or GCC.

[quote author=Nick Gammon link=topic=192910.msg1425653#msg1425653 date=1381559130] This is an incredibly interesting project, of course. I love the way the internal state machine has been reverse engineered.

http://www.pagetable.com/?p=517 [/quote]

Yes, I saw this a while after I wrote the emulator. Really interesting low-level stuff!! I originally wrote this as part of a NES emulator. Of course, the Arduino could never pull that off. Something like the Apple ][ may be do-able.

Is it possible to view the preprocessor output directly? That may give some hints as to what's going wrong.

Attach your code to a post, and then we'll take it from there.

Sorry, here it is as an attachment.

arduino-6502.c (23.1 KB)

static void and() {

“and” is a C++ reserved word.

http://en.cppreference.com/w/cpp/keyword

If you make a new tab in the IDE and call it whatever.c, and then paste your code into it, and add this line:

#define NULL (void *)0

And make your main sketch:

void setup ()
  {
  }  // end of setup

void loop ()
  {
  }  // end of loop

Then it compiles. There are some warnings you may want to check out if you enable verbose compiling.

Thanks for the help, but I’m still getting the same problems.

Most of the warnings are expected due to most of the 6502 opcode emulation functions not being directly called in the code. There are function pointer arrays to the proper addressing mode functions and instruction emu functions for each opcode value. The execution loop can be tiny due to this, but it has the side-effect of the compiler thinking most functions aren’t called thus the warnings. Instead of a huge switch block like you see in some CPU emulators, the main loop is reduced to this:

        (*addrtable[opcode])();
        (*optable[opcode])();
        clockticks6502 += ticktable[opcode];

I thought this was the best way to keep a very small code size, and it makes it a lot more maintainable.

I guess let’s start with the first error. This line:

    write6502(BASE_STACK + ((sp - 1) & 0xFF), pushval & 0xFF);

Generates this error:

_6502_c:84: error: expected unqualified-id before '&&' token

And there is no && in my code. BASE_STACK is simply a define of 0x100. So, something is happening in the preprocessor that is breaking things. I wonder what of my code it doesn’t like.

This is getting REALLY strange. Even if I comment out the lines that get errors and recompile, it still reports errors on the comments! :~

Can you describe exactly your compiling process? As I said, I compiled under the IDE using the method I described that error was not present.

And there is no && in my code. BASE_STACK is simply a define of 0x100. So, something is happening in the preprocessor that is breaking things. I wonder what of my code it doesn't like.

The line number being reported is clearly wrong. Using my method it reported the function I mentioned.

The file iso646.h might be responsible:

#ifndef __cplusplus
#define and &&
#define and_eq  &=
#define bitand  &
#define bitor   |
#define compl   ~
#define not !
#define not_eq  !=
#define or  ||
#define or_eq   |=
#define xor ^
#define xor_eq  ^=
#endif

After pre-processor substitution the use of the word "and" could result in an error about "&&".

The line it errored on for me, compiling the way I suggested, was:

static void and() {

Example:

static void and ()
  {
  }

void setup ()
  {
  }  // end of setup

void loop ()
  {
  }  // end of loop

Error:

sketch_oct13a:2: error: expected unqualified-id before '&&' token
sketch_oct13a:1: error: expected unqualified-id before '&&' token

Thanks for spending some more time on this. I’m not doing anything special. Just clicking verify. I fixed the “and” thing, and now I’m getting this error first:

_6502_c:84: error: expected unqualified-id before 'asm'

Line 84 is completely blank. I’m attaching the current file.

_6502_c.ino (22.2 KB)

I think the reported line numbers are off.

I've changed the following things in "arduino-6502.c":

  • added "#define NULL 0" to the top
  • renamed the "and" function to something else ("Xand")
  • made the tentative definitions of "addrtable" and "optable" in lines 113/114 "extern" instead of "static", and removed "static" from their initializations in lines 746/766
  • added a typecast to line 866: "loopexternal = (void (*)()) funcptr;"

What remains are a few warnings, but the code compiles then. Whether it works, I can't tell. ;-)