Vscode Cc65 Debugger

Aayushi Singh

Vscode Cc65 Debugger


This is an extension to let you debug the CC65 C code (and ASM code to a small degree) made for the Commodore platforms and NES, including the Commodore 64, using a VICE emulator and VS Code.

To make sure all the features work, you'll want to install VICE 3.5 or later. A modified build of CC65 for Linux and Windows is already included, so you'll only need to specify your own if you're using a platform such as Mac OS.

Windows-specific instructions You will need to install VICE 3.5 or later and make. A modified build of CC65 is included. The easiest way to install VICE to your PATH is to use Chocolatey.

Make sure you use an Administrator shell!

Skip this command if you have Chocolatey already.

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Install the packages

choco install --version 3.5.2021010401 winvice-nightly choco install make Make sure these directories are not on your PATH. They will break the Makefile:

C:\Program Files\Git\bin C:\Program Files\Git\usr\bin These directories are okay:

C:\Program Files\Git\cmd Linux-specific instructions (Debian [and probably also Ubuntu]) You will need to install VICE 3.5 or later and make. A modified build of CC65 is included.

To install VICE 3.5 before it is packaged for your distribution, you will need to build VICE from source, to do that, download the source from the VICE website, then follow the below steps:

sudo apt install build-essential checkinstall subversion sudo apt build-dep vice cd vice-3.5 ./autogen.sh ./configure make -j$(nproc) sudo mkdir -p /usr/local/share/{vice/C64,doc/vice} && sudo checkinstall -y --exclude=/home --install=yes --pkgname=vice --pkgversion=3.5.0 --summary='VICE is a Commodore 64 emulator. This is a version I built to be able to use new features required by VSCode.' --provides=vice --requires='libasound2, libatk1.0-0, libc6, libcairo-gobject2, libcairo2, libfontconfig1, libgcc1, libgdk-pixbuf2.0-0, libgl1, libglew2.1, libglib2.0-0, libgtk-3-0, libjpeg62-turbo, libpango-1.0-0, libpangocairo-1.0-0, libpng16-16, libpulse0, libreadline7, libstdc++6, zlib1g' --nodoc make install The last two commands will take a while, but afterwards VICE should be installed.

Project Configuration After installing go to your launch.json and create a new section using the snippet. If you don't have a launch.json, the "create a launch.json file" link in the debug section should create a simple one.

Obsolete settings:

viceCommand: Please see the setting cc65vice.viceDirectory in your user settings. Required settings for both launch and attach:

name: The name in the debug dropdown menu. request: launch will launch, attach will attach. type: Always cc65-vice for this debugger. build: Attributes for the build command. You need this for attachment as well, so the debugger can find relative paths in your debug file. command: Your actual build command. Defaults to make if unspecified. You will need to change your Makefile to support being debugged with this. args: An array with args for your command. Defaults to [] if unspecified. cwd: The working directory for your build command. You need this for attachment as well, so the debugger can find relative paths in your debug file. skip: Should we skip building before execution? Required for attach mode only:

port: The port to attach to in attach mode. This is the port configured with VICE's -binarymonitoraddress option. If you specify this for a launch, it will force it to use that port instead of a random one. Required for launch mode only:

emulatorArgs: You'll want to set your C64 model here, and any other special hardware options that you need for your program. Either NTSC or one of the PAL models (jap, drean, etc). Look at the VICE manual for the full list. Other shared settings:

stopOnEntry: This will break at the beginning of the program. Otherwise it will continue automatically after the debugger connects. stopOnExit: This will break at the end of the program. Otherwise it will terminate automatically. program: Specify this if the debugger can't find your binary file. By default it will look for a d81/d64 and if it can't find any a PRG. If you have multiple of those types of files, it will try some fanciness to determine which one is the "real" one, such as looking at the modification date and how many files are in your disk image, but those may fail. mapFile: Manually specify the path of your map file. Use this if auto detection fails. When this is unset it will look for a file in the same folder as your program named PROGRAMNAME.map debugFile: Manually specify the path of your debug file. Use this if auto detection fails. When this is unset it will look for a file in the same folder as your program named PROGRAMNAME.dbg There are also some user settings to note: cc65vice.enableCycleCounters: Enable cycle counters next to each code line. cc65vice.viceDirectory: Set this to specify the directory that contains the VICE executables. You'll probably need this on Windows. If this is omitted then it will look on the system PATH. cc65vice.mesenDirectory: Set this to specify the directory that contains the Mesen executables. You'll probably need this on Windows. If this is omitted then it will look on the system PATH. cc65vice.cc65Home: Set this to specify the directory that contains the CC65 build. This is the CC65_HOME directory, and not the bin directory, so the folder above bin. If your system doesn't have prebuilt binaries, you probably want to use CC65 on your PATH, and not this. cc65vice.preferX64OverX64sc: Set to true to use x64, which is not recommended. cc65vice.disableMetrics: This disables metric reporting, which tracks when the extension is activated or a debug session is requested or fails. cc65vice.runAhead: When hitting a breakpoint, step ahead by one frame so that any screen updates that may have been made become visible immediately. You may have some problems with autostart-warp working correctly. The way VICE detects this may be to blame. To turn it off, just add +warp and +autostart-warp to your emulatorArgs:

{ ... "emulatorArgs": [ "+autostart-warp", "+warp", "-model", "ntsc" ] ... } Changes needed to your Makefile If you've used the default Makefile at the CC65 project wiki, it's required to use a slightly modified Makefile.

If instead you made a custom Makefile, you will need to tell the linker that you want a debug file and a map file. You would add the following options to your linker:

-g -Wl "--mapfile,build/PROGRAMNAME.map" -Wl "--dbgfile,build/PROGRAMNAME.dbg" And the following to your compiler:

--debug-tables "FILENAME.tab" Make sure that the paths on the files are in the same folder and have the same name (minus the extension, of course) as your main program!

If you have included any optimizations (-Osir) you should probably turn those off, however, effort has been made to trace some of them.

You may also want to look at the full Assembly project template, and the C project template.

Please also note the mk.sh and mk.bat. These will help you run make (or anything else with minor modifications) from the proper context outside of vscode, so that you use the builtin cc65 compiler when possible, instead of the one installed on your machine.

What works Starting the program and stopping at the beginning of main() Setting and consistently hitting breakpoints Stepping over lines. Stepping out of functions. Stepping into functions. Array types Viewing structs Local variables Static local variables Global variables Registers Variable and memory drilldown. With any variable, even ones that were determined to be a single byte, you can use it as a two byte pointer to jump to other blocks of memory by expanding the dropdown triangle. Once you get to actual memory, you'll get 8x16 bytes (128 bytes). You can expand a row of 8 bytes and it will give you 8 options for pointer referencing (01,12,23,34,...) which you can expand and get another 128 byte chunk of memory, and on and on forever... Pausing and resuming works pretty much as you might expect. If you pause in the middle of a library function it will look strange but the state of VICE will be okay, but that's pretty typical for this sort of thing (think Microsoft's scary "no symbols for this file" window when you pause on a compiled DLL in VS proper). Building You will need node >=13, vscode >=1.42, pnpm >=5.5, Python 3 (to generate the font), and Subversion. Jest Test Explorer extension is also recommended.

For more details about what is needed to build, please look at the Dockerfile

To test:

pnpm install --shamefully-hoist pnpm build:full pnpm test To build, run the following commands:

pnpm install --shamefully-hoist pnpm build:full If you want to debug the extension, use the Extension + Server debug target in VSCode after running the build commands above at least once. This will start two debug sessions. You will need to restart the Server if you change any code called by the main debug session process. Basically anything referenced by src/dbg/runtime.ts.

You will need to restart the Extension if you change anything in the Extension which is not UI code. All the UI side code is located under src/webviews/stats-webview-content.ts since there is only one screen at the moment. All the non-UI code is under src/ext.

If you change UI code while debugging, it will automatically be reloaded. However you may need to close the webview and restart the cc65-vice session to get it to reappear correctly.

Some other package.json scripts of note:

clean: Will remove all generated outputs such as files in dist. Use this If something doesn't seem to update. distclean: Will remove all files including node_modules and the 3rdparty directories. Use this if something is really sideways. compiler: Will build all the versions of cc65 to dist/cc65 compiler:quick: Will only build the x86 versions of cc65 for your platform. This is used when you launch the project in VSCode. webpack and webpack:debug: Builds all the webpacked parts of the project. The debug adapter, monitor, extension, and extension UI code. tisk: Builds all the source with standard tsc. Called by Ctrl+Shift+B in VSCode. This doesn't actually build the project, just quickly verifies that the code isn't broken. Also used before the Webpack build so each component doesn't revalidate the syntax, making it faster overall. build:full: Builds everything for deployment. build:test: Only builds the parts needed to run the tests, and to debug the extension in VSCode. vscode:prepublish: Used for vsce packaging. Will run the tests before generating a vsix file. vscode:prepublish:github: Only called on the build server. Skips the tests since they are run separately as a PR check. vice:choose: Selects a version of VICE to test against. Look at build.env.sample lint: Project linting. Not as important since TypeScript itself prevents a lot of obvious mistakes. jest: Run only some of the tests. For example: pnpm jest -- src/tests/runtime-other-platforms.test.ts jest:compile: Runs only the compile tests. This is separate because the other tests are dependent on the cc65 projects being compiled. jest:noncompile: The tests that come after compilation. test: Run the tests in the correct order. Does not build anything, so use build:test for that. testmerge: Runs each batch of tests twice, then combines the results, preferring a successful test run. This is because the tests sometimes behave erratically on the build server and I want to avoid running the entire job again. It will also print out the name of each test and whether any of the two runs of it passed. package: Run vsce package using pnpm. The project is using a modified version of vsce that has support for pnpm.