Monday, January 1, 2024

Retrospective XV

Last year, I conducted a simple retrospective for 2022. Therefore, here is a retrospective for 2023.

2023 Achievements
  • Complete fifteen year blog on agile software application to game development and beyond
  • Enter Lil Evel Knievel in SMS Power! 2023 competition as side scrolling Wonderboy tribute
  • Port Sega Master System games to Sega MegaDrive then document development process
  • Apply Sega Master System development techniques to eBPF setup using C code examples
  • Resurrect test driven development and mock integration into Linux / C environment setup
  • Transition 8-bit Sega Master System development setup to the Nintendo Game Boy+Color
  • Prepare Python environs for OpenAI retro video game Reinforcement Learning integration
  • Experiment with SerenadeAI and prototype ChatGPT APIs across multiple language range

Note: leveraging Sega Master System development setup for Nintendo Game Boy is an achievement!

2024 Objectives
  • Resume Microservices application development and deploy across Distributed Systems at scale
  • Apply Infrastructure as Code and adopt DevOps best practices as software grows in complexity
  • Extend Software Engineering experience across Design + Architectural responsibilities in Cloud
  • Integrate Artificial Intelligence to Software projects: Machine, Deep or Reinforcement Learning

Architecture
Software architecture plays crucial role in designing + developing effective software systems. Understanding various architectural styles + patterns available, you can make informed decisions in architectural endeavors to create scalable, maintainable, and reliable software solutions. Architectural styles are high-level strategies that provide an abstract framework for a family of systems e.g. Layered, Event-Driven, and Microservices.


Architect Types
Software architects, solution architects and enterprise architects all play a part in designing, developing and implementing solutions that meet the needs of the business but differ with technical, company and IT focus:
 Software
 Architect 
 A professional who designs and oversees the development of each software system.
 Ensures that the system meets the needs of the business and is technically feasible.
 Solution
 Architect 
 A professional who designs and implements solutions that meet needs of the business.
 They have a deep understanding of both the business and the technology landscapes.
 Enterprise
 Architect 
 A professional who designs and oversees the overall IT architecture of an organization.
 Ensures architecture aligned with business strategy and supports goals and objectives.


Cloud Variants
Cloud computing is becoming common for accessing data and using appliations in web-based environments. Growing demand for technology solutions in this area request need for cloud architects and cloud engineers:
 Cloud
 Architect 
 A professional who deploys cloud-based applications according to business specifications.
 Architects focus work on the frameworks and infrastructure of cloud-based applications.
 Cloud
 Engineer 
 A professional who designs, creates and develops cloud-based systems and solutions.
 Involved in building and testing technical requirements within all these cloud systems.


Microservices
Microservices have become a game-changer in the quickly changing technological landscape where flexibility, speed and scalability are crucial. Microservices architecture provides a more agile and effective substitute to conventional monolithic techniques and has seen rapidly increasing industry adoption in recent years.

Microservices align seamlessly with DevOps principles, fostering development and operations collaboration for continuous delivery and automation. Microservices architecture also aligns with containerization and orchestration tools like Kubernetes, simplifying the deployment and management of services at scale.

Industry predictions in Microservices expect growth to $1.7b by 2028 as 82% of organizations plan to adopt microservices in the next 3yrs. Microservices are well-suited to leverage emerging technologies such as IoT, edge computing, and blockchain and A.I. has potential to disrupt and innovate in the microservices space.


Future
The Top Tech Skills for 2024 corroborates this as specialization in the cloud computing market is predicted to grow at an annual rate of 18% plus Artificial Intelligence and Machine Learning continue to drive innovations across all industries including using AI in Microservices; AI is becoming an essential part of the microservices ecosystem, enabling businesses to build and maintain large, complex applications more efficiently. It will be interesting to see new innovative ways in which AI may integrate across the Software Engineering spectrum!

Wednesday, November 15, 2023

GBDK Programming Sample

In the previous post, we checked out GBDK Programming Setup. The GBDK is a cross-platform development kit for sm83, z80 and 6502 based gaming consoles including the Nintendo Game Boy and Game Boy Color.

Using the GBDK, it is now possible to write game code using the C language rather than pure Z80 assembly. Therefore, we would like to extend this knowledge and checkout some code samples and homebrew games.

Let's check it out!

Software
Follow all instructions from the previous post: this documents how to setup all the pre-requisite software.
Note: ensure you have downloaded and installed the GBDK devkit and Small Device C Compiler [SDCC].

Examples
As per the previous post, after the GBDK has been downloaded, installed and setup, we should now checkout C:\gbdk\examples\gb to learn more about APIs available in the devkit. Replicate prior Hello World example.

Launch Visual Studio 2015. File | New | Project... | Visual C++ | Win32 | Win32 Project
 Name:  Game
 Location:  C:\apa_image
 Create directory for solution  UNCHECKED

Copy over all original folders and files from the corresponding example. Copy from prior Hello World example the hidden .vscode folder and files. Copy also build.bat and align with source compile.bat file. Add relevant files to Visual Studio 2015. Press Ctrl+1 to build, link and run code similar to the prior Hello World example!

Launch Visual Studio Code. Open example folder. Set breakpoint in main.c. Press F5 to debug step thru code


Here is the complete list:
 CUSTOM: apa_image SOURCE: apa_image
 CUSTOM: banks SOURCE: banks
 CUSTOM: bcd SOURCE: bcd
 CUSTOM: colorbar SOURCE: colorbar
 CUSTOM: comm SOURCE: comm
 CUSTOM: crash SOURCE: crash
 CUSTOM: dscan SOURCE: dscan
 CUSTOM: filltest SOURCE: filltest
 CUSTOM: galaxy SOURCE: galaxy
 CUSTOM: gb-dtmf SOURCE: gb-dtmf
 CUSTOM: gbcompress SOURCE: gbcompress
 CUSTOM: incbin SOURCE: incbin
 CUSTOM: irq SOURCE: irq
 CUSTOM: isr_vector SOURCE: isr_vector
 CUSTOM: large_map SOURCE: large_map
 CUSTOM: lcd_isr_wob SOURCE: lcd_isr_wob
 
 CUSTOM: linkerfile SOURCE: linkerfile
 CUSTOM: metasprites SOURCE: metasprites
 CUSTOM: paint SOURCE: paint
 CUSTOM: ram_function SOURCE: ram_function
 CUSTOM: rand SOURCE: rand
 CUSTOM: rpn SOURCE: rpn
 CUSTOM: scroller SOURCE: scroller
 CUSTOM: sgb_border SOURCE: sgb_border
 CUSTOM: sgb_multiplay SOURCE: sgb_multiplay
 CUSTOM: sgb_pong SOURCE: sgb_pong
 CUSTOM: sgb_sfx SOURCE: sgb_sfx
 CUSTOM: simple_physics SOURCE: simple_physics
 CUSTOM: sound SOURCE: sound
 CUSTOM: temp_minimal SOURCE: temp_minimal
 CUSTOM: temp_subfolder SOURCE: temp_subfolder
 CUSTOM: wav_sample SOURCE: wav_sample

Samples
Gaming Monsters YouTube channel has many samples to teach how to develop your own GameBoy games:

Launch Visual Studio 2015. File | New | Project... | Visual C++ | Win32 | Win32 Project
 Name:  Game
 Location:  C:\SimpleSprites
 Create directory for solution  UNCHECKED

Copy over all original folders and files from the corresponding example. Copy from prior Hello World example the hidden .vscode folder and files. Copy also build.bat and align with source's make.bat file. Add relevant files to Visual Studio 2015. Press Ctrl+1 to build, link and run code similar to the prior Hello World example!

Launch Visual Studio Code. Open example folder. Set breakpoint in main.c. Press F5 to debug step thru code


Here is the complete list:
 CUSTOM: helloworld SOURCE: helloworld
 CUSTOM: simplesprites SOURCE: simplesprites
 CUSTOM: movesprites SOURCE: movesprites
 CUSTOM: background SOURCE: background
 CUSTOM: window SOURCE: window
 CUSTOM: sound SOURCE: sound
 CUSTOM: noise SOURCE: noise
 CUSTOM: simplejumping SOURCE: simplejumping
 CUSTOM: metasprites SOURCE: metasprites
 CUSTOM: spritecollision SOURCE: spritecollision
 CUSTOM: bkgdcollision SOURCE: bkgdcollision
 
 CUSTOM: splashscreens SOURCE: splashscreens
 CUSTOM: fadeeffects SOURCE: fadeeffects
 CUSTOM: keyboard SOURCE: keyboard
 CUSTOM: savename SOURCE: savename
 CUSTOM: colorsprites SOURCE: colorsprites
 CUSTOM: backgrounds SOURCE: backgrounds
 CUSTOM: makingmusic SOURCE: makingmusic
 CUSTOM: spritetricks SOURCE: spritetricks
 CUSTOM: scrolling SOURCE: scrolling
 CUSTOM: debugging SOURCE: debugging
 CUSTOM: platformer SOURCE: platformer

Homebrew
Larold's Jubilant Junkyard is a website/blog/shop which focuses on creating digital products including game development projects and tutorials. The Nintendo GameBoy development tutorial series is very educational.

Follow Larold's Jubilant Junkyard YouTube video channel for additional GBDK 2020 information and content:
 SCREEN resolution  20 x 18 tiles  160 x 144 pixels
 BACKGROUND  32 x 32 tiles  256 x 256 pixels

Compiler flags
 -Wm-yc  GameBoy Color compatible  NB: lowercase c
 -Wm-yC  GameBoy Color only  NB: lowercase C
 -Wm-ys  Super GameBoy compatible  

GameBoy Color Tile Attributes
 Bit 7  priority flag  0: tiles below sprites, 1: tiles above sprites
 Bit 6  vertical flip  0: tiles normal, 1: flipped vertically
 Bit 5  horizontal flip  0: tiles normal, 1: flipped horizontally
 Bit 4  not used  
 Bit 3  character bank specification  0: tiles at bank0, 1: tiles at bank1
 Bit 2  see bit 0  
 Bit 1  see bit 0  
 Bit 0  bits 0-2  indicate tile color palette assignment

Launch Visual Studio 2015. File | New | Project... | Visual C++ | Win32 | Win32 Project
 Name:  Game
 Location:  C:\AlleywayGameboy
 Create directory for solution  UNCHECKED

Copy over all original folders and files from the corresponding example. Copy from prior Hello World example the hidden .vscode folder and files. Copy also build.bat and align with source's make.bat file. Add relevant files to Visual Studio 2015. Press Ctrl+1 to build, link and run code similar to the prior Hello World example!


NOTE: ensure that the GBDK header files are included for Intellisense: Right click project | Properties |
Configuration Properties | C/C++ | General | Additional Include Directories | %GBDK_WIN%/include; $(SolutionDir)/headers/gen; $(SolutionDir)/headers/main; %(AdditionalIncludeDirectories).

Here is the complete list:
 CUSTOM: alleyway  SOURCE: alleyway
 CUSTOM: flappy-bird  SOURCE: flappy-bird
 
 CUSTOM: pacman  SOURCE: pacman
 CUSTOM: space-invaders  SOURCE: space-invaders

Summary
Armed with all this knowledge, we are now in an excellent position to build complete video games for the Nintendo Game Boy and Game Boy Color. In the interim, we see the GameBoy Competition 2023 is here!

Friday, September 15, 2023

GBDK Programming Setup

In 2017, we checked out devkitSMS Programming Setup to build 8-bit video games for Sega Master System. Here we installed all the tools required including a powerful emulator: Emulicious to test the video games.

Emulicious is a free multi-system emulator popular for Sega Master System and Sega Game Gear games but can also play Nintendo Game Boy and Game Boy Color games. Therefore, let's leverage Sega Master System development setup to build and test games for Nintendo Game Boy and Game Boy Color using the GBDK.

Let's check it out!

GDDK
The GBDK is a cross-platform development kit for sm83, z80 and 6502 based gaming consoles including the Nintendo Game Boy and Game Boy Color. It includes libraries, toolchain utilities and SDCC C compiler suite.

Software
Follow all instructions from the previous post: this documents how to setup the pre-requisite software.

Here is a summary of all required software to be installed:
 Name Version
 C IDE Editor Visual Studio 2015
 C IDE Editor Visual Studio Code
 Cross compiler Small Device C Compiler
 
 Name Version
 Make files Cygwin
 Emulators Emulicious, BGB
 Debugger Emulicious Debugger
Note: this post documents how to download and install SDCC v4.1.0 on both Windows and Linux.

GBDK
Navigate to the gbdk-2020 repository on github: gbdk-2020 has full instructions here. Download the latest release from gbdk-2020 archive e.g. gbdk-4.1.1 or git clone latest source code. Extract code into C:\gbdk.

Setup the following environment variable for GBDK_WIN as it can be useful throughout GBDK development. Add the following one environment variable: System | Advanced system settings | Environment Variables:
 Variable  Value  Description
 GBDK_WIN  C:\gbdk  Windows path format


Example
As an example, let's write a simple program that prints Hello World to the screen using the GameBoy DevKit. Create new directory: C:\HelloWorld. Create new file main.c. Copy in the following code to print Hello World.

main.c
#include <gb/gb.h>
#include <stdio.h>

void main()
{
	printf( "HELLO WORLD" );
}

Build
Manually compile and link the Hello World program. Launch command prompt: Start | Run | cmd.


Change directory cd C:\HelloWorld. Next, execute the following 2x commands (in bold):
 ACTION  COMMAND  OUTPUT
 Compile   %GBDK_WIN%/bin/lcc -Wa-l -Wl-m -Wl-j -c -o main.o main.c  main.o
 Link  %GBDK_WIN%/bin/lcc -Wa-l -Wl-m -Wl-j -o output.gb main.o  output.gb

Finally, type output.gb. The Hello World program should launch in the Emulicious emulator.
Congratulations! You have just written your first test program using the GameBoy DevKit.

IMPORTANT
If game does not initially render correct then select Options menu | System | Game Boy or Game Boy Color.

Automate
Let's automate the build process: create C:\HelloWorld\build.bat script file that contains the commands:
@echo off
%GBDK_WIN%/bin/lcc -Wa-l -Wl-m -Wl-j -c -o main.o main.c
%GBDK_WIN%/bin/lcc -Wa-l -Wl-m -Wl-j -o output.gb main.o
output.gb

Visual Studio 2015
Similar to Sega Master System development using devkitSMS, setup Visual Studio 2015 as per instructions here. Also, follow instructions here to connect Ctrl+1 hot key to automatically compile, link and run code.

Launch Visual Studio 2015. File | New | Project... | Visual C++ | Win32 | Win32 Project
 Name:  HelloWorld
 Location:  C:\
 Create directory for solution  UNCHECKED

 Application type:  Console application
 Additional options:  Empty project CHECKED

Navigate to C:\HelloWorld. Add existing: main.c and build.bat at the same folder level as HelloWorld.sln.


Finally, ensure that the GBDK header files are included for Intellisense: Right click project | Properties |
Configuration Properties | C/C++ | General | Additional Include Directories | %GBDK_WIN%/include.

Press Ctrl+1 to build, link and run!


Visual Studio Code
Similar to Sega Master System development using devkitSMS, setup Visual Studio Code as per instructions here. Add hidden .vscode folder + include tasks.json and launch.json files beneath to build + debug code.

Launch Visual Studio Code. Open folder C:\HelloWorld. Add the .vscode folder the following files beneath:

c_cpp_properties.json
{
  "configurations": [
    {
      "name": "stevepro",
      "intelliSenseMode": "clang-x64",
      "includePath": [
        "${env:GBDK_WIN}/include"
      ],
      "cStandard": "c11",
      "cppStandard": "c++17",
      "browse": {
        "path": [
          "${workspaceFolder}"
        ],
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": ""
      }
    }
  ],
  "version": 4
}

tasks.json
{
    "version": "2.0.0",
    "label": "build",
    "type": "shell",
    "windows": {
        "command": "./build.bat"
      },
    "presentation": {"echo": true, "reveal": "always", "focus": false, "panel": "shared", "showReuseMessage": false, "clear": true},
    "group": {"kind": "build", "isDefault": true},
}

launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "emulicious-debugger",
            "request": "launch",
            "name": "Launch in Emulicious",
            "program": "${workspaceFolder}/output.gb",
            "port": 58870,
            "stopOnEntry": false,
        }
    ]
}

Open main.c. Set breakpoint. Press Ctrl + Shift + B to build source code. Press F5 to debug step thru code:


In Emulicious, choose Tools menu | Debugger to be able to fully debug step thru the Z80 assembly code:


Summary
Now the GBDK has been downloaded, installed and setup, we should checkout C:\gbdk\examples\gb to learn more about the APIs available in the devkit + checkout some code samples and complete homebrew games. This will be the topic of the next post.

Thursday, August 31, 2023

eBPF Programming Sample

In the previous post, we checked out eBPF Programming Setup. Extended Berkeley Packet Filter [eBPF] is technology that runs sandboxed programs in privileged context like the OS kernel to safely and efficiently extend the capabilities of the kernel at runtime without requiring changes to the kernel source code. One issue is there are currently limited methods to debug BPF programs thus we'd like to explore test options.

Let's check it out!

Software
Follow all instructions from the previous post to install CLion and VS Code on Ubuntu Linux. Also install the following components for low level C code unit testing, mocking and integration: Unity, CMock, Ceedling.
 Unity   Unit testing framework built for C projects with a focus on working with embedded toolchains
 CMock   Mock and stub generator and runtime for unit testing C designed to work smoothly with Unity
 Ceedling   Build system for C projects using Ruby Rake [make] build system to integrate Unity + CMock

Pre-requisites
As above, Ceedling requires Ruby installed otherwise sudo: gem: command not found errors may be thrown. Also, CMock requires Ruby bundle be installed otherwise Command 'bundle' not found errors may be thrown.
 sudo apt install ruby  sudo apt install ruby-bundler

Unity
Unity is a unit testing framework built for C, with a focus on working with embedded toolchains, which we would like to try and leverage for BPF development. Follow instructions here to install and configure Unity.
 git clone git@github.com:ThrowTheSwitch/Unity.git
 cd Unity
 cmake .
 make
 sudo make install
 sudo cp libunity.a /usr/local/lib
 sudo cp src/unity*.h /usr/local/include/unity



CMock
CMock is a mock and stub generator designed to work smoothly with Unity. CMock automagically parses the C headers and creates useful mock interfaces used for unit testing. Follow instructions here to install CMock.
 git clone --recursive https://github.com/throwtheswitch/cmock.git
 cd cmock
 bundle install


Ceedling
Ceedling is a build system for C projects using Ruby Rake [make] build system. Ceedling also makes TDD Test Driven Development simple by integrating CMock + Unity. Follow instructions here to install Ceedling.
 ruby -v
 sudo gem install ceedling




Hello Unity
In the previous post, examples installed the libbpf C-based library. Therefore, extend an example and write tests that invoke simple libbpf APIs, for example, libbpf_num_possible_cpus directly using Unity framework.

Launch CLion | New Project | C Executable | Location: /home/stevepro/HelloUnity | Language std: C11

Create

Organize src and test directory structure. Install libbpf beneath src folder. Rename main.c to all_tests.c and move beneath test folder. Update all prod and unit test code. Complete CMakeLists.txt to include library files:
 cmake_minimum_required(VERSION 3.19)
 project(HelloUnity C)
 set(CMAKE_C_STANDARD 11)
 
 include_directories(src/ /usr/local/include/unity/ src/include/libbpf/src)
 link_directories(/usr/local/lib/ src/include/libbpf/src/amd64)
 
 file(GLOB CODE_FILES src/*.c)
 file(GLOB TEST_FILES test/*.c)
 add_executable(HelloUnity ${CODE_FILES} ${TEST_FILES} )
 target_link_libraries(HelloUnity unity bpf elf z)

Edit configurations | Run with root privileges | Set breakpoints | Press F5 to debug step thru source code.



Hello CMock
At the time of writing, there is currently no way to debug BPF programs' source code interactively. Therefore, we would like to attempt to mock BPF programs or at least underlying C sourc code somehow using CMock.

Follow the example Spin up CMock. This article uses Ceedling to automate project setup and generate mocks but we will complete the following manually first to better understand the process. Original code found here.

Open folder /home/stevepro/HelloCMock. Copy inc/rectangle.h + project.yml. Execute mock command:
 ruby ~/GitHub/ThrowTheSwitch/cmock/lib/cmock.rb -oproject.yml inc/rectangle.h

This creates mocks folder two corresponding files: mock_rectangle.h and mock_rectangle.c. Notice we did not attempt to use CLion as is not currently integrated; any attempt may produce Multiple Definition issue!


Hello Ceedling
Complete previous example but this time try Ceedling using the article as a guideline: Launch the Terminal:
 cd $HOME
 ceedling new HelloCeedling

Copy all project files into src and test directories. Include inc and update project.yml with the inc directory:
  :paths:
  :test:
    - +:test/**
    - -:test/support
  :source:
    - src/**
  :include:
    - inc/**
  :support:
    - test/support
  :libraries: []

Run tests manually simply by typing ceedling

Debug tests interactively with GDB as follows: Launch VS Code. Press F5 to add launch.json file to complete:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "ceedling_gdb",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/test/out/test_shape_container.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
Set breakpoint in test_shape_container.c | Press F5 to debug step thru source code:



Summary
According to ChatGPT, eBPF is versatile technology that allows safe and efficient code execution within the Linux kernel without requiring modification of kernel code. This feature makes it attractive for applications including network packet filtering, security, logging + tracing, monitoring, and performance optimization.

In summary, while eBPF is a valuable addition to the Linux ecosystem, it is not expected to replace kernel development. Instead, eBPF complements this by currently providing a flexible and efficient way to extend specific kernel features. Therefore, it will be interesting to see if this technology is dominant in the future.

Tuesday, July 4, 2023

eBPF Programming Setup

Extended Berkeley Packet Filter [eBPF] is a technology that runs sandboxed programs in privileged context like the OS kernel to safely and efficiently extend the capabilities of the kernel at runtime without requiring changes to the kernel source code. libbpf is a C-based library that takes compiled BPF object files and loads and verifies them into the kernel attachiing BPF programs to various kernel hooks and execute custom code.

Let's check it out!

Software
Follow all instructions from the previous post to install CLion and VS Code on Ubuntu Linux. Also install the bpftool which was added to the 2017 Linux kernel as a user space utility to introspect the eBPF subsystem.
 git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
 cd linux
 cd tools/bpf/bpftool
 make
 sudo make install
 bpftool version


Alternatively, download bpftool binary from the Github releases and extract into /usr/local/bin directory.

Hello World
Create your first BPF program using this simple eBPF CO-RE application as a reference however using CLion. Launch CLion | New Project | C Executable | Location: /home/stevepro/HelloWorld | Language std: C11

Create

A new C project will be created which automatically adds the following two files: main.c and CMakeLists.txt
 main.c  CMakeLists.txt
 #include <stdio.h>
 int main()
 {
   printf( "Hello, World!\n" );
   return 0;
 }
 cmake_minimum_required(VERSION 3.19)
 project(HelloWorld C)
 
 set(CMAKE_C_STANDARD 11)
 
 add_executable(HelloWorld main.c)

In the Terminal window within CLion IDE enter the following commands to install and buld the libbpf library:
 git clone https://github.com/libbpf/libbpf
 cd libbpf/src
 make
 sudo make install
 cd ../..

Next, enter commands to create the vmlinux.h file. Create and build the Hello BPF program + skeleton file:
 bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h
 touch hello.bpf.c
 clang -g -O2 -target bpf -D__TARGET_ARCH_x86_64 -I libbpf/src -c hello.bpf.c -o hello.bpf.o
 bpftool gen skeleton hello.bpf.o > hello.skel.h

 hello.bpf.c  CMakeLists.txt
 #include "vmlinux.h"
 #include "bpf_helpers.h"
 
 SEC("tracepoint/syscalls/sys_enter_execve")
 int hello_bpf(struct trace_event_raw_sys_enter *ctx)
 {
   bpf_printk("Hello world!\n");
   return 0;
 }
 char LICENSE[] SEC("license") = "GPL";
 cmake_minimum_required(VERSION 3.19)
 project(HelloWorld C)
 
 set(CMAKE_C_STANDARD 11)
 
 include_directories(.)
 link_directories(libbpf libbpf/src)
 
 add_executable(HelloWorld main.c)
 target_link_libraries(HelloWorld bpf elf z)

Rename main.c to hello.c and update the driver program code. Edit configurations | Run with root privileges

Set break point in hello.c | HelloWorld | Debug or Press F5 to launch your first Hello World BPF program!

IMPORTANT you can tail BPF output in Terminal window: sudo cat /sys/kernel/debug/tracing/trace_pipe

Hello Maps
BPF maps are generic structures for storing different types of data accessible between kernel + user space. Launch CLion | New Project | C Executable | Location: /home/stevepro/HelloMaps | Language std: C11

Create

Repeat the process similar to Hello World: Install the libbpf library. Create the vmlinux header file, the Maps BPF program and driver code. Compile code. Update the CMakeLists build script and Run with root privileges:


libbpf-bootstrap
libbpf-bootstrap provides a starting point for building your own BPF applications with things like BPF Compile Once-Run Everywhere [CO-RE] and vmlinux.h. Numerous more BPF samples can be found at the Linux repo.
 CUSTOM: minimal  SOURCE: minimal
 CUSTOM: minimal_ns  SOURCE: minimal_ns
 CUSTOM: minimal_legacy  SOURCE: minimal_legacy
 CUSTOM: bootstrap  SOURCE: bootstrap
 CUSTOM: uprobe  SOURCE: uprobe
 CUSTOM: usdt  SOURCE: usdt
 
 CUSTOM: fentry  SOURCE: fentry
 CUSTOM: kprobe  SOURCE: kprobe
 CUSTOM: ksyscall  SOURCE: ksyscall
 CUSTOM: tc  SOURCE: tc
 CUSTOM: profile  SOURCE: profile
 CUSTOM: sockfilter  SOURCE: sockfilter

Summary
Although libbpf allows BPF programs to be loaded, verified and attached to various kernel hooks and execute custom code there is currently no way to debug BPF programs' source code interactively. Therefore, this now presents alternatives to explore unit testing and mock BPF programs. This will be the topic of the next post.

Thursday, June 1, 2023

Sega Console Game Port

In 2017, we checked out devkitSMS Programming Setup to build code in C for the 8-bit Sega Master System. Then, in 2020, we checked out SGDK Programming Setup to build code in C for the 16-bit Sega Mega Drive.

Leveraging this knowledge, we would now like to port existing code built for the 8-bit Sega Master System, for example, Simpsons Trivia SMS to the 16-bit Mega Drive Simpsons Trivia MD and document the process.
 SEGA MASTER SYSTEM [256 x 192]  SEGA MEGA DRIVE [320 x 224]
Let's check it out!

Software
Follow most instructions from the previous post: this documents how to setup some pre-requisite software.

Here is a summary of some of the software to be installed:
 Name Version
 C IDE Editor Visual Studio 2015
 Emulators Fusion, Gens KMod
 
 Name Version
 Cross compiler GCC
 Make files Cygwin


Setup Project
Create folder C:\SimpsonsTriviaMD. Copy new SGDK custom library lib folder here. Create sub-folder: dev. Change directory to dev folder and create the following sub-folders as indicated per Usage: inc, out, res, src


Launch Visual Studio 2015. File | New | Project... | Visual C++ | Win32 | Win32 Project

 Name:  Game
 Location:  C:\SimpsonsTriviaMD\dev
 Create directory for solution  UNCHECKED
OK

 Application type:  Console application
 Additional options:  Empty project CHECKED
Finish

First, remove x64 build configuration! Right click Solution | Properties. Click Configuration Manager button. Click Active solution platform drop down | Edit. Click x64 option | Remove. Now will have only Win32 build.

Right click Project | Properties | Configuration Properties | General. Set Output and Intermediate directories:

 Output Directory:  $(SolutionDir)..\bin\$(ConfigurationName)
 Intermediate Directory:  $(SolutionDir)..\obj\$(ConfigurationName)

Right click Project | Properties | Configuration Properties | C++ | General. Set Additional Include Directories:

 Value:   $(SolutionDir)..\lib;$(ProjectDir)inc;$(ProjectDir)res;$(GDK_WIN)\inc;$(IncludePath)

Right click Project | Properties | Configuration Properties | Linker | General | Additional Library Directories:

 Value:   $(SolutionDir)..\lib

Right click Project | Properties | Configuration Properties | Linker | Input. Set the Additional Dependencies:

 Value:   _genesis.lib;%(AdditionalDependencies)

The final solution should look like this:


Setup Resources
Import all resources into your project as media files that your game will use such as images for background tiles + sprites, audio for music + sound effects etc. Clone the "Setup Project" folder and follow these steps:

Change directory to res folder under dev folder. Copy all resources here. Create corresponding *.res files. Next execute build.bat to compile all resources then generate all corresponding gfx and sfx header files.

Create folder gfx at same level as dev folder. Create sub folder res. Add dummy corresponding translation unit files under res sub folder. Repeat process with sfx folder. Import all translation units into Visual Studio.


Setup Folders
Larger game projects will require nested folders in the inc or src directories to organize engine and screen code etc. Clone the "Setup Resources" folder and add all necessary subfolders + then update the makefile.

Launch Visual Studio and include resource files as before. Next, scale out engine and screen code but now organize into subfolders. Create as New Filters under Visual Studio Header Files and Source Files solution:

Create corresponding engine and screen subfolders beneath inc folder under dev. Add header files. Create engine and screen subfolders beneath src folder under dev. Add all the corresponding translation unit code.

Right click Project | Properties | Configuration Properties | C++ | General. Set Additional Include Directories:

 Value:   $(SolutionDir)..\lib;$(ProjectDir)inc;$(ProjectDir)res;$(GDK_WIN)\inc;$(IncludePath);
 $(ProjectDir)inc\engine;$(ProjectDir)inc\object;$(ProjectDir)inc\screen

Customize the makefile.gen file to include the new subfolders accordingly. Copy %GDK_WIN%\makefile.gen to local dev folder. Update makefile: Add new $(wildcard $(SRC)/ and -I$(INCLUDE) entries per folder.

Update dev built.bat file to use new local custom makefile.gen instead of de-facto %GDK_WIN% version.
If you remove pre-build step to speed up makefile build then don't forget to create out subfolders manually.


Convert APIs
Finally, copy over all engine, object, screen etc. files from the Sega Master System code base into the Sega Mega Drive port. Convert all devkitSMS APIs to the SGDK equivalents. Allow for different screen resolution!

Here is quick cheat of devkitSMS to SGDK API conversions; of course not all APIs are one-to-one mapping:
   SEGA MASTER SYSTEM  SEGA MEGA DRIVE
 Audio  PSGPlay();
 PSGStop();
 PSGGetStatus();
 XGM_startPlayPCM();
 XGM_stopPlayPCM();
 XGM_isPlayingPCM();
 Dimensions  256 x 192 [32x24]
 320 x 224 [40x28]
 Font  SMS_setNextTileatXY();
 SMS_setTile();
 VDP_setMapEx();
 
 Graphics  SMS_loadPSGaidencompressedTiles();
 SMS_loadBGPalette();
 VDP_drawImageEx();
 VDP_setPalette();
 Hack  Address : 0x0050
 Address : 0x01E0
 Input  SMS_getKeysStatus();
 JOY_readJoypad();
 Random  rand();
 random();
 Sprites  SMS_addSprite();
 SMS_copySpritestoSAT();
 SPR_addSprite();
 SPR_update();


Summary
Repeat process with Skazka. In summary, the Master System to Mega Drive port should be fairly seemless J
 SEGA MASTER SYSTEM [256 x 192]  SEGA MEGA DRIVE [320 x 224]