My First Steps with the X16: Questions and goals!

Chat about anything CX16 related that doesn't fit elsewhere
sebassco
Posts: 23
Joined: Wed Aug 26, 2020 4:02 am

My First Steps with the X16: Questions and goals!

Post by sebassco »


Hi everyone!

This may be a long one... so hang on tight cuz' I don't have a TL;DR ?  Although, if you just want to take a look my questions, you can check that at the end of the post.

 

I am looking to understand the first steps I (or, in any case, anyone) should take while starting to use the X16 and how to develop any kind of stuff for it. In fact, if things go as expected I  am thinking of using this thread as a kind of log about my journey through the X16 developing world ?. This is in order to achieve a greater goal, an educational one, not just for me but for anyone that finds this interesting.

So, let's begin with a short(ish) introduction!

Main goal: From what I've seen on the videos Dave uploaded to his channel, I get that the computer should be "simple" enough for someone with some knowledge to understand what is going on at any moment without having to worry about other much more complicated concepts. Basically what I see about it is that it should let me build things that emulate how an OS works in real life or, even better, work like that. It may sound a little boring or even non-ambitious at all,  but after getting there I will obviously set other goals in the future. Right now my main focus is on learning and building things that help me get to a more in-depth understanding.

My background: I am 30 years old and work focused mainly on Integration (REST APIs, SOAP, etc). Basically some of the products I worked with tend to have a kind of unique way of solving their deployment challenges, including being non-dependant to a JVM and run a direct communication with the OS to manage their processes and access memory. That means I usually take into account how things work inside the server I am using and what would be the way to extract the best behaviour out of it, what relates to the other thing I've done in my career: getting to understand how OS's work and, more important, learning about low-level coding. From my experience as a student (I'm still working on getting my degree), I think there are features anyone could take advange of with the X16. So, yeah, it should be mainly about software, and very little about hardware... well, just enough hardware in order to be able to do what I plan to do.

My experience with retro computers: Given my age you would think it's not great but it is something. Well... I have no experience at all. My dad had a 386 clone running DOS back when I was 5, but after that we jumped directly to pentiums and such. I did use plenty of DOS and Windows 3.1.

Skills I have and stuff I do know about and think will be useful:


  • (And I think this is an important one)


  • Process management (Multithreading, planning, etc.)


  • File systems (NTFS, FAT32, EXT2)


Skills I do not have and stuff I do not know about and wish to learn:


  • 6502 architecture


  • Assembly (I'm guessing this is a must)


  • Basic (I am reading about that at the moment)


  • cc65


Skills I do not have and stuff I do not know about but are not a priority:


  • GUI design


  • Graphics in general


What I do not care about (for the time being, at least):


  • Games: I know this may be a bummer to many of you and will stop reading after looking at this, but while I love retro games, I am not interesting in developing them.


  • In-depth knowledge of hardware: By this I mean soldering, boards, chips, etc. I do not have any skills for that and never will, so I better keep it real and learn about what each component does without caring about how they are built and wired in.


  • Building an entire OS: It is not my scope at the moment, and tbh I don't know if it will ever be. But dreaming is always allowed, right? lol.


 

Ok, enough of introduction. Let's pan out my current location on this "journey":

What resources have I checked? What was the outcome of that?

I checked the FAQ, Docs, and the "Getting Started" Wiki. I don't know if I am short of knowledge right now or if I need things layed out differently but while I got to run things on the emulator (programs of the software library and some small basic tests), I haven't been able to understand yet what should I do as a first "hello world" creation using something else apart from BASIC.

What have I done? What am I doing at the moment?

So, as I said before, I've been reading all the docs and tried some software from the library besides trying some basic examples on my own. Right now I am trying to learn a little bit about KERNAL and BASIC with the C64 user guide in addition to the X16 Programmer's reference in order to get a little more experience on how to operate the X16.

What's the primary objective right now?

To get something running that has been written on my own. Just a basic program asking for an input and returning something. But I don't know if I should change this, maybe you can help me with my questions.

 

Here are all the questions I have at the moment


  • Should I bother to learn BASIC or maybe should I get straigh into C? I am skipping assembly for now, but will tackle that in the future.


  • If the previous answer is C : How should I start? I've seen the cc65 webpage, but should I take something else into account? Is there a "hello world" example or something like that to get into terms with the compiler and such?


  • Is process management a too far-fetched concept with the 6502? I'm guessing that if C is usable, I could use concepts as threads, but I am hesitant about that being doable on that CPU.


 

DISCLAIMER: I haven't seen any posts like this one, or at least haven't found them. If you deem this is a "repost" please let me know and accept my apologies ?

 

Ok, I really hope you find this interesting.

Let the learning begin!

Ender
Posts: 220
Joined: Sat May 09, 2020 9:32 pm

My First Steps with the X16: Questions and goals!

Post by Ender »


Definitely learn BASIC.  The thing runs on a BASIC interpreter. You need to know how to make the machine do things outside of just loading in your compiled program and running it.  Writing some BASIC programs to play around with graphics, etc. is a good way to get a feel for how the machine works and the ways to make it do things.

For C, honestly? Not a lot of people are using C right now. Probably 90% of the people messing around with the X16 are using BASIC or assembly. I think I've seen one game written in C (there are probably more though).  cc65's X16 support is a bit shaky.  As development of the ROM continues (although I think the major breaking changes are over/close to being over) cc65's X16 support will break and you'll have to keep waiting for fixes or fix them yourself. For assembly, though, support doesn't break since it's just dependent on the 65C02 instruction set.  I think also, in order to understand what your program is doing, or why your program will have to do a lot of seemingly random placing of values into certain memory addresses, it's good to learn the assembly first so you can understand what the heck is going on in a X16 C program.

For threads, I'm pretty sure it's possible but it would be very tricky to do.  It's definitely something the 6502/65C02 wasn't designed for, and not something natively supported in the C.

sebassco
Posts: 23
Joined: Wed Aug 26, 2020 4:02 am

My First Steps with the X16: Questions and goals!

Post by sebassco »


Cool, thanks a lot, @Ender! Will take that into account.

ChrisL
Posts: 30
Joined: Tue Apr 28, 2020 12:17 am

My First Steps with the X16: Questions and goals!

Post by ChrisL »


As one of the folks using C for the X16 I'd say don't discount it.  There are a couple of C examples in the x16-demo repo (https://github.com/commanderx16).  I've done a Lode Runner port and a utility library in C (https://github.com/CJLove/x16-LodeRunner).  The number of available registers on the 6502 don't make it a great cpu for compiled languages like C.  But just like the X16's higher clock speed enables a lot more to be done in BASIC that couldn't be done on the C64 it gives a margin for cases of less than stellar code generation by CC65.

 

User avatar
AndyMt
Posts: 326
Joined: Sun Jun 21, 2020 3:02 pm
Location: Switzerland

My First Steps with the X16: Questions and goals!

Post by AndyMt »


I'm also one of those using cc65 and implement my games for the X16 in C. Sometimes there is a need of implementing some optimized parts in assembler (interrupt handlers, sound effects player, etc.). cc65 has the advantage that combining C and assembler is easy. I sometimes check the generated assembly code, to see what the C compiler does and the hand-optimize some of it. Or I change the way I do my C code.

I also didn't experience any case of cc65 being really broken... if so, it was possible to fix it myself as the source is on github.

Basic was too limited for me and I wanted the comfort and usability of cross development tools on a PC - and then run it on the emulator. So I went for Visual Studio Code and cc65.

But hey - the beauty of the X16 is that there are different ways of programming for it.

SerErris
Posts: 172
Joined: Sat Aug 08, 2020 9:18 am

My First Steps with the X16: Questions and goals!

Post by SerErris »


Just want to cover some thoughts on the threading.

Threading has come up with CPUs with a lot of cycles and the main routine sitting around most of the time and idling. Also having multiple cores requires threads as otherwise you would not be able to use it in a single program. Threads come with at least two disadvantages:

1. you need to make your code thread save. As threads will run asynchronous, you cannot forecast when a thread will finish its task and if this is inline with the main loop. So you need to write message queues and handlers and such things to ensure that you have everything in the right order. That will put a lot of overhead on both CPU and RAM. Even if RAM is not a big issue any longer with the X16, CPU cycles are precious.

2. You need a MMU or you need to program one yourself to ensure that threads are always accessing their memory. 

Regarding the first point: 

X16 has a very small CPU that is not powerful at all. Yes it is 8times faster than the C64, but the compute power on your hands is less than an Arduino (ESP-12f for instance with WIFI and USB and other components runs at 80Mhz and with much better CPU. 4USD price range ? ) . So if you are starting to use thing like threads you need to use Interrupts, each of the interrupt handlers need to decide what to do (actually multitasking/multithreading) and then save everything somewhere (stack), load the current thread environment and start executing, until the next interrupt happens. Depending on the complexity you are wasting 50% of the available cycles just for the thread handler. Remember the CPU and the whole System has no support of that ready. No multi tasking, No multi threading. No MMU.

So given all that, it is definitely possible to implement multithreading in any CPU as long as you have enough memory to handle it (and you have on X16), however there is most likely nothing that justifies the overhead. 

 

Instead you write a main loop (game loop) and branch into all the individual "tasks" and ensure that it is as efficient as possible and that whatever task you have to complete within a frame fits into the available cycles of a frame (e.g. 8.000.000 cycles/60 =  133k cycles per frame. If you consider that you need at least 2 cycles per instruction, that is not a lot of things you can do within that time frame. (C64 had only 16k cycles per Frame).

User avatar
desertfish
Posts: 1095
Joined: Tue Aug 25, 2020 8:27 pm
Location: Netherlands

My First Steps with the X16: Questions and goals!

Post by desertfish »


Actually, you don't need a MMU, if you are okay with a system that doesn't have memory protection. (Wich is a weird thing to expect from  a 8 bit system anyway..)

I believe the only thing that you have to do is:


  • save cpu state including status flags on a context switch


  • give each thread access to their own hardware cpu stack.


The second point means that you have to write stack swapping code on a plain 65(c)02 because it has its stack fixed at $0100 in memory at all times. I believe the Commodore-128 had a tiny MMU that allowed to remap the zeropage and stack to another page in memory, but I am not sure about this.

Here is a multitasking unix-like operating system for the Commodore-64 that uses multiple threads and stack swap trickery: http://www.6502.org/users/andre/osa/index.html

It's quite unbelievable to see it in action to be honest    https://youtu.be/jtlAOdJmeDI

sebassco
Posts: 23
Joined: Wed Aug 26, 2020 4:02 am

My First Steps with the X16: Questions and goals!

Post by sebassco »


Hey guys, I am really glad you all joined the discussion ?

Thank you very much for all your suggestions. Yes, I was taking into consideration that threading should be a tough call considering the lack of MMU. But, enhancing what I said in the first post, it is not about doing something 'elegant' or even super functional, but instead it is about seeing what can be done and to what extent.

 

@desertfish those links are awesome! Thanks a lot!

 

So, everyone, regarding the first question then: What should I start with? I still reading the C64 manual and probably will end it by next week. From what I see there are not a lot of things in common between both languages but it seem BASIC should give me a better handle of the computer, right? In the other hand, I am starting to think that in order to use cc65 I wil have to take a look at assembly first, is that true?

User avatar
AndyMt
Posts: 326
Joined: Sun Jun 21, 2020 3:02 pm
Location: Switzerland

My First Steps with the X16: Questions and goals!

Post by AndyMt »



7 minutes ago, sebassco said:




In the other hand, I am starting to think that in order to use cc65 I wil have to take a look at assembly first, is that true?



No, you don't need assembler at all if you don't want to with cc65. It will for sure offer more performance than Basic.

But if you just want to get going right away - Basic is the right choice. No installation needed, except for the emulator.

sebassco
Posts: 23
Joined: Wed Aug 26, 2020 4:02 am

My First Steps with the X16: Questions and goals!

Post by sebassco »



1 minute ago, AndyMt said:




No, you don't need assembler at all if you don't want to with cc65. It will for sure offer more performance than Basic.



But if you just want to get going right away - Basic is the right choice. No installation needed, except for the emulator.



Awesome, thanks!

Post Reply