Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user eightbitswide ! (Registered 2024-12-24) You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Some sort of multithreading.
2008-04-08 22:51
gregg
Account closed

Registered: Apr 2005
Posts: 56
Some sort of multithreading.

About a week ago the topic multithreading came up on #c-64. So today I gave it a try. However, there's something wrong with my code and I can't really figure out what it is.

A short description: I have a fixed number of threads running and a CIA IRQ deals with context switching in a round-robin fashion. Every IRQ I save all current state data (SP, status register, PC, A, X, Y) in a structure and fetch the state data for the next thread.

In this example the first thread increments the screen background color (fast), while the second thread changes the border background color (slow). However the wait in the second thread runs too fast every other time, and I have no idea why. It's probably something wrong with the context switch stuff, maybe some of you could take a look at it?

Sources are for ACME.

!to "threading.prg",cbm
!cpu 6510
;!source "mylib.a"
!macro basic_header .a, .b, .c, .d {
        *= $0801
        !byte <.eol,>.eol,0,0,$9e
        !text .a, .b, .c, .d
.eol:   !byte 0,0,0
}

num_threads = 2
thread_num = $fd		; current thread number

;--------------------------------------------------------------------------
+basic_header "2", "0", "6", "1"

*= $080d

init:	sei
		; set up context switch IRQ
		lda #$35
		sta $01

		lda #<context_switch
		ldx #>context_switch
		sta $fffe
		stx $ffff
		
		lda #0
		sta thread_num

		cli
		jmp thread1

;--------------------------------------------------------------------------
context_switch:
		pha
		txa
		pha
		tya
		pha
		lda $dc0d

		; save current thread
		lda thread_num
		; *8
		asl
		asl
		asl
		tay
		; save A,X,Y
		pla
		sta thread_data+6,y
		pla
		sta thread_data+5,y
		pla
		sta thread_data+4,y
		; save PSW
		pla
		sta thread_data+1,y
		; save PC
		pla
		sta thread_data+2,y
		pla
		sta thread_data+3,y
		; save SP
		tsx
		txa
		sta thread_data,y

		; next thread, wraparound
		ldy thread_num
		iny
		cpy #num_threads
		bne +
		ldy #0
+		sty thread_num

		; *8
		tya
		asl
		asl
		asl
		tay

		; restore thread data
		; stack pointer first
		lda thread_data,y
		tax
		txs
		; push PC, PSW for RTI
		lda thread_data+3,y
		pha
		lda thread_data+2,y
		pha
		lda thread_data+1,y
		pha
		; push registers
		lda thread_data+6,y
		pha
		lda thread_data+5,y
		pha
		lda thread_data+4,y
		pha

		pla
		tay
		pla
		tax
		pla
		rti
	
;--------------------------------------------------------------------------
thread1:
		inc $d021
		ldy #$02
		jsr wait2
		jmp thread1


thread2:
		inc $d020
		ldy #$80
		jsr wait2
		jmp thread2
		
wait2:
-		ldx #0
		dex
		bne *-1
		dey
		bne -
		rts

;--------------------------------------------------------------------------
thread_data:
	!fill 8, 0
	!byte $ff-$40, $22, <thread2, >thread2, 0,0,0,0

 
... 211 posts hidden. Click here to view all posts....
 
2008-04-17 16:04
chatGPZ

Registered: Dec 2001
Posts: 11386
the difference can only be explained in the context of the execution environment. (to pull my example again, in the OS of the PSP, everything is a "process". a comparable thing with similar properties in the context of a unixish OS would only qualify as a thread)
2008-04-17 16:25
Trash

Registered: Jan 2002
Posts: 122
I guess my ironi tags became hidden...

My personal definition of a thread is that it is a process making some sort of work for the mainprocess. A process is for me a loop that executes somekind of code and it's also able to delegate jobs to other processes in form of threads..
2008-04-17 17:06
Oswald

Registered: Apr 2002
Posts: 5094
"Cant someone explain the difference between threads and processes, please?"

"the difference can only be explained in the context of the
execution environment."

--> as gregg's code doesnt implement execution enviroment it cannot be said wether it is multitasking or multithreading.

which is the same I keep telling here: if you dont define process: you have no processes -> without process you cant define threads.

like in maths for the concept of complex numbers you need to define: natural numbers -> negative numbers -> rational numbers -> irrational numbers -> complex numbers.

without defining first negative, rational, etc numbers complex numbers cannot be defined.
2008-04-17 17:10
chatGPZ

Registered: Dec 2001
Posts: 11386
Quote:

as gregg's code doesnt implement execution enviroment it cannot be said wether it is multitasking or multithreading.


LOL. how exactly would that code code run at all if there is no execution environment?

you.are.confused.
2008-04-17 17:18
Oswald

Registered: Apr 2002
Posts: 5094
Quote: Oswald, the whole of your posts show why you are wrong because what you are writing contradicts the links you have been posting.
You said "try reasoning that will help". The point is many other posters have tried reasoning with you and what you do is still keep on repeating your same mistaken assumptions and posting the same links that disprove what you are writing.

It is very obvious that you are confused and you are wrong.


"Multi-tasking = Multiple processes that can support multiple threads. A hierarchy."

still waiting you quoting a reliable source which states the same.

no 2 level hierarchy of processes/threads means multitasking without multithreading not what gregg's doing or you trying to force. threads without processes simply make no sense. by definition threads are parts of processes and by definition processes are part of multitasking capable OSes.

2008-04-17 17:22
Oswald

Registered: Apr 2002
Posts: 5094
Quote: Quote:

as gregg's code doesnt implement execution enviroment it cannot be said wether it is multitasking or multithreading.


LOL. how exactly would that code code run at all if there is no execution environment?

you.are.confused.


you said that processes and threads cannot be told appart without execution enviroment. I have assumed you are using "execution enviroment" in the sense of an OS. Since you have even brought an example where you compared 2 OS as execution enviroments. Hardware may be looked at like an execution enviroment, but the cpu/ram/bus/hdd type alone wont help tell apart threads and processes imho.
2008-04-17 17:55
chatGPZ

Registered: Dec 2001
Posts: 11386
you are, again, assuming too much and drawing conclusions from said assumptions. "execution environment" has nothing to do with OS, threads, processes - or even computers per se.
2008-04-17 18:11
Oswald

Registered: Apr 2002
Posts: 5094
"Cant someone explain the difference between threads and processes, please?"

groepaz: "the difference can only be explained in the context of the execution environment."

groepaz: " "execution environment" has nothing to do with ... threads, processes "

conclusion: the difference between threads and proccesses can only be explained only in the context of the execution enviroment which has nothing to do with threads, processes.

thats supposed to make some sense?

what is this magical execution enviroment, it has nothing to do with processes and threads, still in its context the difference can be told between processes and threads? can you define this magical concept for me? :)



2008-04-17 18:27
JackAsser

Registered: Jun 2002
Posts: 2014
I've been thinking about this for a while now and without citing neither Google nor Wikipedia nor refer to any textbook I came to my personal conclusion which is ofcource highly subjective:

The C64 clearly has an OS. This OS takes care of IO, keyboard, cursor blink, screen scrolling and even some rudimentary memory managment if you consider the built in BASIC.

Natrually I consider this a non multitasking OS despite it being capable of f.e. formatting a disk while allowing the user to type in BASIC code. I consider it non multitasking because there are no OS-calls to create new processes. Only replace the one and only existing via LOAD+RUN.

Still this one and only process can very well use the OS to hook its own timer IRQ and implement threading by simple context switching and still be friend with the OS. This kind of threading is normally called green threads, i.e. threads implemented non OS-code.

So to conclude what I consider:

* The KERNEL is an OS.
* The OS is non multitasking allowing only one process.
* _THE_ process can implement threads any way it wants.

What do you guys think? Did I put more wood in the fire now?! ;)
2008-04-17 18:38
chatGPZ

Registered: Dec 2001
Posts: 11386
Quote:

conclusion: the difference between threads and proccesses can only be explained only in the context of the execution enviroment which has nothing to do with threads, processes.

thats supposed to make some sense?


no, your conclusion, again, doesnt make sense.
Previous - 1 | ... | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
SLC
aeeben
Lead/House Designs
CreaMD/React
MWR/Visdom
Guests online: 113
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 The Demo Coder  (9.6)
6 Edge of Disgrace  (9.6)
7 What Is The Matrix 2  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 Layers  (9.6)
2 Cubic Dream  (9.6)
3 Party Elk 2  (9.6)
4 Copper Booze  (9.6)
5 X-Mas Demo 2024  (9.5)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Censor Design  (9.3)
5 Triad  (9.3)
Top Original Suppliers
1 Derbyshire Ram  (9.7)
2 Fungus  (9.3)
3 Black Beard  (9.2)
4 Baracuda  (9.2)
5 hedning  (9.1)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.058 sec.