Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
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-18 08:29
Martin Piper

Registered: Nov 2007
Posts: 722
Quote: Having read the last few days of posts, the problem isn't that anyone is completely incorrect here - Oswald is correct in many ways - the problem is that these concepts are inherently difficult.

The important thing to realize is that there is nothing magical about these concepts: an operating system is not a magical entity, it is just another program. A process is just a convenient definition that does not necessarily have anything to do with operating systems, but that also has changed its meaning over the years. Wikipedia is not the authoritative source. Multithreading is nothing to brag about, it is just another programming tool.

There is only one really good way to grasp these things: read a couple of good books on the subject of programming and operating systems (Knuth, Tanenbaum, Silberschatz/Galvin are good names to know), read a couple of good books on concurrent programming languages, write a couple of operating systems, write a couple of multithreading libraries, write a couple of compilers, learn how different CPU architectures work by doing low-level programming on them, step-by-step debug multithreaded code in a debugger, design a CPU architecture and think about interrupt handling and stacks, and constantly challenge your own presumptions. After doing this a few times one usually finds out that these concepts are not as easy and clear as one initially thought :)


Yes, Oswald is lacking the experience to be able to make these distinctions and instead resorts to trying to cite different parts of Wikipedia without understanding the much bigger picture.

Although I have to say a multi-threading system with multi-tasking processes is something to brag about as it's quite a lot of code to implement memory management, hardware resources management etc. ;)
2008-04-18 08:54
Frantic

Registered: Mar 2003
Posts: 1648
This THREAD is closed. Admit it!
2008-04-18 09:54
Zyron

Registered: Jan 2002
Posts: 2381
The thread is closed but the process continues...
2008-04-18 10:08
Martin Piper

Registered: Nov 2007
Posts: 722
ExitThread(-1);

? ;)
2008-04-18 11:50
Oswald

Registered: Apr 2002
Posts: 5094
Quote: Yes, Oswald is lacking the experience to be able to make these distinctions and instead resorts to trying to cite different parts of Wikipedia without understanding the much bigger picture.

Although I have to say a multi-threading system with multi-tasking processes is something to brag about as it's quite a lot of code to implement memory management, hardware resources management etc. ;)


Oh my dear. You lack the defined experience aswell, and you lack the brains to realize that aswell. Your only goal left is discredit me by saying your mantra "you are wrong, confused, etc" instead of having arguments that makes any sense. Groepaz its time to close the topic, as it is entering personal only stage.
2008-04-18 12:47
Martin Piper

Registered: Nov 2007
Posts: 722
You are wrong, I have the experience and the brains.
It is obvious that you do not because you are resorting to personal attacks instead of refuting the argument put before you. Your own posts discredit yourself and your "argument" because what you have been posting is demonstrably incorrect and the links you have been posting also show you are wrong. Lastly, the other posters in this thread are also telling you the same thing, that you are wrong. Yet you still continue to post the same rubbish.
2008-04-18 13:05
Oswald

Registered: Apr 2002
Posts: 5094
No I have the experience and brains and you dont, furthermore you are wrong because your own explanations proove you wrong. also you know you are wrong, but you dont admit that so you are trolling. now go back to kinder/trollgarten with this style of arguing dumbfuck.
2008-04-18 14:53
Martin Piper

Registered: Nov 2007
Posts: 722
Your repeated personal attacks demonstrate you're trolling and that your "argument" has failed.
2008-04-18 16:25
Oswald

Registered: Apr 2002
Posts: 5094
Your disability to refuse my arguments prooves that u r wrong. Oh forgot to add ur fave "point": it prooves aswell u r trolling.
2008-04-18 18:47
Slammer

Registered: Feb 2004
Posts: 416
It's clear that this discussion has lost all form of relevant content. Please stop it before you loose the respect you earned in other contexts.
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
Lord Crucifier/SCS*TRC
ciccior2003/HF
Jack the Rapper
ΛΛdZ
kbs/Pht/Lxt
algorithm
Guests online: 99
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 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 X-Mas Demo 2024  (9.5)
7 Dawnfall V1.1  (9.5)
8 Rainbow Connection  (9.5)
9 Onscreen 5k  (9.5)
10 Morph  (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 Graphicians
1 Mirage  (9.8)
2 Archmage  (9.7)
3 Pal  (9.6)
4 Carrion  (9.6)
5 Sulevi  (9.6)

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