VSCode just gets better and better. I’ve been using it as my editor for the Acorn Electron replay core for a while now but always tabbed out to the terminal to run the build script and impact gui to program the FPGA.

No longer! This can all be done now from within VSCode via ctrl+shift+b to build and ctrl+shift+p to program the FPGA.

VSCode has a feature called “Tasks” which you can use to run arbitrary commands. A tasks file can be created in your VSCode workspace via Tasks/Configure Tasks/Other.

The following image shows the output of a failed build task (invoked by ctrl+shift+b) in the terminal tab.

ctrl+shift+b terminal output

ctrl+shift+b terminal output

Build / Program Task

For reasons that are not really relevant to this post, I have my vscode workspace one directory above the acorn_electron core folder. The tasks file temporarily changes the working directory to acorn_electron before building, programming or finding source files to account for this.

If the workspace is shifted to within the core directory the whole task can be made more generic and apply to any core.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "Build Bitstream",
            "type": "shell",
            "command": "(cd acorn_electron && ./build.sh)",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true,
                "panel": "shared"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": {
                "owner": "vhdl",
                "fileLocation": ["relative", "${workspaceRoot}/acorn_electron/source/"],
                "pattern": {
                    "regexp": "^(ERROR|WARNING):HDLParsers:\\d+\\s-\\s\".*build/(.*)\"\\sLine\\s(\\d+).\\s+(.*)$",
                    "file": 2,
                    "line": 3,
                    "severity": 1,
                    "message": 4
                }
            }
        },
        {
            "taskName": "Program Device",
            "type": "shell",
            "command": "(cd acorn_electron && impact -batch impact.cmd)",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true,
                "panel": "shared"
            }
        }
    ]
}

With the above tasks file in your workspace, ctrl+shift+b will switch to the acorn_electron directory and run the build.sh (refer to the Replay SVN repo for build.sh contents) which in turn runs each Xilinx command line tool to generate the core’s bitstream file.

If the build succeeds, ctrl+shift+p will run the “Program Device” tasks which invokes the impact tool with a batch file to program the FPGA via the JTAG “Platform Cable USB”.

If the build fails, ctrl+shift+m will bring up the problems tab where you can then click any error/warning and jump to the file/line as shown in the below image.

ctrl+shift+m problems listing - jump to file/line

ctrl+shift+m problems listing - jump to file/line

There’s one quirk with how Replay cores build. All vhdl files have their hierarchy flattened and are copied into a build/ directory. The path the error refers to is thus to the build/ directory copy of the original source file and not the file you would want to edit.

To account for this, the regular expression only captures the file name and not the full path to the file. Then the fileLocation entry makes the assumption that the file is in the source/ directory.

"fileLocation": ["relative", "${workspaceRoot}/acorn_electron/source/"],

Clearly this is a less than ideal workaround as any error in a source file within a sub-directory or the replay library when selected will fail to open. That however is preferable to opening the build/ directory version, fixing the error and then finding the file is overwritten with the next build. Most new errors will be in the core sources anyway :)

Short of changing how the replay uses the build directory such that pathing information is retained, this seems hard to avoid and an acceptable compromise.

Note, this only catches compile errors. It will not display Xst errors/warnings such as signals with multiple drivers.

Program Device Short-cut

In order to get ctrl+shift+p to run the program device task, open your user keybindings.json (File/Preferences/Keyboard shortcuts, then select advanced settings) and add the line:

{ "key": "ctrl+shift+p",       "command": "workbench.action.tasks.runTask", "args": "Program Device" },

ctrl+shift+b already exists as long as you set the “Build Bitstream” task as your default build task.

Impact Batch

The impact.cmd to program the device via the USB cable is short and sweet:

# Impact command line batch file
# Usage: impact -batch impact.cmd

setmode -bscan
setcable -p auto
addDevice -p 1 -file "build/acorn_electron.bit"
program -p 1 
quit

With the exception of the file path workaround, this makes for quite a nice workflow improvement compared to tabbing out, looking for error messages, tabbing back, opening the file, jumping to the line, forgetting what the error message said…