| | 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.... |
| | Martin Piper
Registered: Nov 2007 Posts: 739 |
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. ;)
|
| | Frantic
Registered: Mar 2003 Posts: 1661 |
This THREAD is closed. Admit it! |
| | Zyron
Registered: Jan 2002 Posts: 2381 |
The thread is closed but the process continues... |
| | Martin Piper
Registered: Nov 2007 Posts: 739 |
ExitThread(-1);
? ;)
|
| | Oswald
Registered: Apr 2002 Posts: 5127 |
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. |
| | Martin Piper
Registered: Nov 2007 Posts: 739 |
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.
|
| | Oswald
Registered: Apr 2002 Posts: 5127 |
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.
|
| | Martin Piper
Registered: Nov 2007 Posts: 739 |
Your repeated personal attacks demonstrate you're trolling and that your "argument" has failed. |
| | Oswald
Registered: Apr 2002 Posts: 5127 |
Your disability to refuse my arguments prooves that u r wrong. Oh forgot to add ur fave "point": it prooves aswell u r trolling. |
| | Slammer
Registered: Feb 2004 Posts: 449 |
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 | |