Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user Coinoperator ! (Registered 2024-06-17) 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-15 09:11
chatGPZ

Registered: Dec 2001
Posts: 11154
thank god you are not trolling, i am!
2008-04-15 09:55
Stryyker

Registered: Dec 2001
Posts: 465
That is different. IRQ effectively does a SEI for you. CLI just reverses it. It isn't a mode. It is a flag like Osawld has said.
2008-04-15 10:08
chatGPZ

Registered: Dec 2001
Posts: 11154
check again how 6502 works
2008-04-15 10:12
Martin Piper

Registered: Nov 2007
Posts: 647
Quote: That is different. IRQ effectively does a SEI for you. CLI just reverses it. It isn't a mode. It is a flag like Osawld has said.

It *is* a different operating mode or "operating level" as I use the term.

In one mode/level "the gap" between one instruction and another can be interrupted, by the IRQ line on the CPU being held low. In the other operating level/mode the IRQ line being held low will not interrupted the execution of the code in "the gap" between instructions.

I say "the gap" but really it happens at the fetch stage of the opcode.
2008-04-15 10:44
Oswald

Registered: Apr 2002
Posts: 5032
now define what do you mean under operating mode. as its generally used in the context I have copyed from WP for you above. also a special feature of different operating modes/privilege levels is that only the highest (kernel mode) level can change privilege. (otherwise it would be meaningless to force tasks into various modes, as they could break the mode) so in this context an irq shouldnt be able to do a cli itself. anyhow even if it couldnt do a cli it were just a dumb irq flag anyway which doesnt affects the cpu's behaviour like privilege modes do, irqs are irqs and not threads or a special privilege level or whatever the next zealot will come up with.
2008-04-15 10:51
Oswald

Registered: Apr 2002
Posts: 5032
Quote: It *is* a different operating mode or "operating level" as I use the term.

In one mode/level "the gap" between one instruction and another can be interrupted, by the IRQ line on the CPU being held low. In the other operating level/mode the IRQ line being held low will not interrupted the execution of the code in "the gap" between instructions.

I say "the gap" but really it happens at the fetch stage of the opcode.


for god's sake, read this, and stop talkign nonsense.

http://en.wikipedia.org/wiki/CPU_modes

2008-04-15 10:53
chatGPZ

Registered: Dec 2001
Posts: 11154
no, the irq flag per se doesnt affect the cpu operation mode - it indicates what operation mode the cpu currently runs in though =P (and operation modes have little to do with privilegues. except privilegued modes are operation modes - but operation modes arent necessarily privilegued)
2008-04-15 11:15
Martin Piper

Registered: Nov 2007
Posts: 647
Quote: for god's sake, read this, and stop talkign nonsense.

http://en.wikipedia.org/wiki/CPU_modes



That link does not disprove what I have been writing. By posting that link you are demonstrating that you do not understand the subtle differences, Groepaz pointed this out as well.

I already defined what is meant by "operating level" in posts 80 and 85. You are incorrectly conflating privilege level with operating level by introducing different terminology compared to what I have been using.

You need to completely ignore anything you have been writing about privilege levels because it is not relevant to what I have been posting.
2008-04-15 13:17
Skate

Registered: Jul 2003
Posts: 492
In PC world, if you need to add an FTP/HTTP component and if that component locks up your program while connecting/transfering data etc, it's time for you to learn about multi-threading. If you are using 6510, what are you going to do with it? You want to download something from a BBS while rotating a filled cube in the screen?

Of course I know multi-threading has many different use alternatives. But if you are thinking about an irq loader and an effect on the screen, what's new about this? why do we need to name it?

I don't agree with Oswald at some points but I can understand why he's fighting against naming this method "multi-threading".
2008-04-15 13:46
Martin Piper

Registered: Nov 2007
Posts: 647
Quote: In PC world, if you need to add an FTP/HTTP component and if that component locks up your program while connecting/transfering data etc, it's time for you to learn about multi-threading. If you are using 6510, what are you going to do with it? You want to download something from a BBS while rotating a filled cube in the screen?

Of course I know multi-threading has many different use alternatives. But if you are thinking about an irq loader and an effect on the screen, what's new about this? why do we need to name it?

I don't agree with Oswald at some points but I can understand why he's fighting against naming this method "multi-threading".


I remember I used multi-threading for a GUI OS on the 64 back in 1991 or '92, I can't remember exactly when. Back then I called it multitasking of course since that is what the Amiga called it.

This had memory management used to allocate memory for each process and when the "EXE" was loaded the OS would relocate the code for you.
Previous - 1 | ... | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | ... | 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
bugjam
blitzed
Beast/Crescent
MaD ][/Starship
Twilight/Excess/Arcade
Guests online: 176
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.7)
5 Edge of Disgrace  (9.7)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Comaland 100%  (9.6)
9 Wonderland XIV  (9.6)
10 Aliens in Wonderland  (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 Rainbow Connection  (9.5)
6 It's More Fun to Com..  (9.5)
7 Dawnfall V1.1  (9.5)
8 Daah, Those Acid Pil..  (9.5)
9 Birth of a Flower  (9.5)
10 Quadrants  (9.5)
Top Groups
1 Nostalgia  (9.4)
2 Oxyron  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 SHAPE  (9.3)
Top Musicians
1 Rob Hubbard  (9.7)
2 Stinsen  (9.7)
3 Jeroen Tel  (9.6)
4 Linus  (9.6)
5 psych858o  (9.6)

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