Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples for llvm-mos c #1

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@ smiley.bas
release/

# IDE files
.vscode
.vscode

# Ignore vim backup files
*~

# LLVM
*.elf
CMakeFiles/
CMakeCache.txt
cmake_install.cmake
build/
85 changes: 85 additions & 0 deletions clang/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
AccessModifierOffset: -2
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: DontAlign
AlignOperands: false
AlignTrailingComments: false
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: AfterColon
BreakInheritanceList: AfterColon
BreakStringLiterals: false
ColumnLimit: 80
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
FixNamespaceComments: true
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*\.h>'
Priority: 1
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 3
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentPPDirectives: BeforeHash
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner
PointerAlignment: Left
ReflowComments: false
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: false
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
25 changes: 25 additions & 0 deletions clang/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
Checks: "*,
-abseil-*,
-altera-*,
-android-*,
-fuchsia-*,
-google-*,
-llvm*,
-modernize-use-trailing-return-type,
-zircon-*,
-readability-else-after-return,
-readability-static-accessed-through-instance,
-readability-avoid-const-params-in-decls,
-cppcoreguidelines-non-private-member-variables-in-classes,
-misc-non-private-member-variables-in-classes,
"
WarningsAsErrors: ''
HeaderFilterRegex: ''
FormatStyle: none

CheckOptions:
- key: readability-identifier-length.IgnoredVariableNames
value: 'x|y|z'
- key: readability-identifier-length.IgnoredParameterNames
value: 'x|y|z'
1 change: 1 addition & 0 deletions clang/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Makefile
39 changes: 39 additions & 0 deletions clang/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Example CMake config for X16 llvm-mos project
cmake_minimum_required(VERSION 3.18)
set(LLVM_MOS_PLATFORM cx16)
find_package(llvm-mos-sdk REQUIRED)
project(clang-examples)

# Only needed if there are C++ source files
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS YES)

# Let's set a rather loud warning level
set(CLANG_WARNINGS
-Wall
-Wextra # reasonable and standard
-Wshadow # warn the user if a variable declaration shadows one from a parent context
-Wnon-virtual-dtor # warn the user if a class with virtual functions has a non-virtual destructor. This helps
# catch hard to track down memory errors
-Wold-style-cast # warn for c-style casts
-Wcast-align # warn for potential performance problem casts
-Wunused # warn on anything being unused
-Woverloaded-virtual # warn if you overload (not override) a virtual function
-Wpedantic # warn if non-standard C++ is used
-Wconversion # warn on type conversions that may lose data
-Wsign-conversion # warn on sign conversions
-Wnull-dereference # warn if a null dereference is detected
-Wformat=2 # warn on security issues around functions that format output (ie printf)
-Wimplicit-fallthrough # warn on statements that fallthrough without an explicit annotation
)
add_compile_options(-mcpu=mos65c02 -Os ${CLANG_WARNINGS})

add_executable(bitmap.prg bitmap.c)
add_executable(plasma.prg plasma.cpp)
add_executable(k_console_test.prg k_console_test.c)
add_executable(k_datetime_test.prg k_datetime_test.c)
add_executable(k_graph_test.prg k_graph_test.c)
add_executable(k_graph_line_test.prg k_graph_line_test.c)
# Add more targets here...

32 changes: 32 additions & 0 deletions clang/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Example X16 llvm-mos CMake C/C++ project

## Steps to build (and run) Commander X16 llvm-mos C/C++ example

This example assumes Linux, macOS or running a *nix like environment under Windows (Like MSYS2, WSL2 or Cygwin64). You will also need CMake installed. Typically you would use your package manager to install CMake (e.g., `apt-get install cmake`, `brew install cmake` or run an installer depending on OS).

1. Download the llvm-mos-sdk package for your OS from <https://github.com/llvm-mos/llvm-mos-sdk/releases/latest>
2. Un-zip or un-tar to the location of your choice and remember the path to that location (e.g.. `llvm-mos` in your home directory).
3. Open a shell prompt in this examples directory (e.g., `x16-demo/clang`)
```sh
cd x16-demo/clang
```
4. Generate the makefile by entering the following commands (change `~/llvm-mos` to where you unpacked the llvm-mos-sdk):
```sh
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=~/llvm-mos ..
```
5. Build by typing `make`
6. To run programs in [x16-emulator](https://github.com/X16Community/x16-emulator/releases/latest), you can use a command similar to the following (this assumes `x16emu` is in your path, if not supply the full path):
```sh
x16emu -scale 2 -prg bitmap.prg -run
```
You could also use another emulator (like [box16](https://github.com/indigodarkwolf/box16) or [BitMagic](https://github.com/Yazwh0/BitMagic)) or copy the `.prg` files to a FAT32 SD card for use on actual Commander X16 hardware.

![bitmap colored line example](bitmap-example.png)
![plasma example](plasma-example.png)

-Xark
<[email protected]>
<https://hackaday.io/Xark>
<https://github.com/XarkLabs>
Binary file added clang/bitmap-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 81 additions & 0 deletions clang/bitmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// llvm x16 test
//
// vim: set et ts=4 sw=4

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

// Commodore common definitions
#include <cbm.h>
// Commander X16 machine definitions
#include <cx16.h>

#include "bitmap.h"

// 320x200 8-bpp video mode
//
// 00000-F9FF 320x200 8-bpp bitmap

#define BITMAP_VADDR 0x00000UL // base VRAM address of bitmap
#define BITMAP_WIDTH 320 // 320 bytes/pixels wide (doubled to add up to 640)
#define BITMAP_HEIGHT 200 // 200 lines high (either doubled or tripled to add up to 480)

void vera_bitmap_320x200_8bpp()
{
VERA.control = 0;
VERA.display.video |= 0b01110000; // enable sprites and layer 0 & 1 (leave video mode untouched)
VERA.display.hscale = 64; // 2x pixel H zoom (320/640 * 128 [128 is 1.0])
VERA.display.vscale = 53; // 2x pixel V zoom (200/480 * 128 [128 is 1.0])

// layer0 is left alone

VERA.layer1.config = 0b00000111; // bitmap | 8-bpp
VERA.layer1.mapbase = 0; // not used for bitmap mode
VERA.layer1.tilebase =
((BITMAP_VADDR >> 11) << 2) | 0; // bitmap address (shifted and aligned), tile-width = 0 for 320
VERA.layer1.hscroll = 0;
VERA.layer1.vscroll = 0;
}

int main(void)
{
static bool done = false;
static uint8_t c;
static uint16_t h;
static uint16_t v;

vera_bitmap_320x200_8bpp();

// set bitmap to colored lines

c = 0; // starting color to fill
while (!done)
{
// set DATA0 to point to start of bitmap, with auto-increment of 1
VERA.control = 0;
VERA.address_hi = VERA_INC_1 | (BITMAP_VADDR >> 16);
VERA.address = BITMAP_VADDR & 0xffff;

// loop over lines
for (v = 0; v < BITMAP_HEIGHT; v++)
{
for (h = 0; h < BITMAP_WIDTH; h++)
{
VERA.data0 = c; // plot pixel (and auto-increment)
}
c++; // next color
waitvsync();

// kernal call to check for key press
if (cbm_k_getin() != 0)
{
done = true;
break;
}
}
}

__asm__ __volatile__ ("jsr $FF81");
}
10 changes: 10 additions & 0 deletions clang/bitmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// llvm x16 test
//
// vim: set et ts=4 sw=4

#pragma once

#include <stddef.h>
#include <stdint.h>

// Nothing much here...
54 changes: 54 additions & 0 deletions clang/k_console_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// llvm-mos-sdk cx16 kernel test
//
// vim: set et ts=4 sw=4

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

#include <cbm.h>
#include <cx16.h>

unsigned char face[64] = {
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 0, 0, 0, 1, 1,
1, 0, 1, 0, 0, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 0, 0, 1, 0, 1,
1, 0, 0, 1, 1, 0, 0, 1,
1, 1, 0, 0, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1
};

void wait_key(void)
{
while (cbm_k_getin() == 0)
;
}

void console_puts(const char *str, unsigned char wordwrap)
{
uint8_t c = 0;
while ((c = *str++) != 0)
{
cx16_k_console_put_char(c, wordwrap);
}
}

int main(void)
{
cx16_k_screen_mode_set(128);
cx16_k_graph_set_colors(1, 0, 0);
cx16_k_console_init(0, 0, 0, 0);
console_puts("\x92Hello Commander X16!", 0);

wait_key();

// BUG: I don't understand why this clears/scrolls screen...
cx16_k_console_put_image(face, 8, 8);

wait_key();

cx16_k_screen_mode_set(0);
printf("DONE.\n");
}
Loading