flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Sound Generation for 64-bit PCs

Author
Thread Post new topic Reply to topic
Adam Kachwalla



Joined: 01 Apr 2006
Posts: 150
Adam Kachwalla 10 Jun 2007, 07:51
Hello. I found the following code designed for 16-bit processors (by the look of it) to generate sound. The question is: how can I generate a tone using the PC Speaker under a 64-bit system? Do they use the same mechanism (PITs, interrupts, etc) as the 16-bit PCs, or is there a better method?
Post 10 Jun 2007, 07:51
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 10 Jun 2007, 08:58
you forgot to attach code?
Post 10 Jun 2007, 08:58
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Adam Kachwalla



Joined: 01 Apr 2006
Posts: 150
Adam Kachwalla 10 Jun 2007, 09:09
Oh right. Here is the 16-bit code that I found (By Matrix, somewhere in this forum and I cannot remember the exact link...):

Code:
; Program: Simple Note Player by MATRIX
org 256
call setup_pit_timer

mov si,msg
call Print
mov bx,tonedata
call Play

call reset_pit_timer
int 20h

;************************************************************
; Procedure print
; prints a zero terminated string pointed to by si
;************************************************************

Print:
push ax
mov ah,14; BIOS code for screen display
cld
print_loop:lodsb; moving the character to be displayed to al
or al, al; checking if the char is NULL
jz printdone
int 10h; Calling BIOS routine
JMP print_loop
printdone:pop ax
ret
; End of print procedure...

;************************************************************
; Procedure Play
; plays a sick and demented melody
;************************************************************
Play:
pusha          ;save regs
localloop:
mov ax,[bx]    ;load sound data
add bx,2
or ax,ax
jz finished
jns notdelay
delay:
push bx
neg ax
mov cx,ax
call pit_tick_delay
pop bx
jmp localloop
notdelay: ; not delay, its a note
push bx
call sound  ;play it
pop bx
jmp localloop
finished:
call nosound
popa
ret
;end Play

nosound: ; Silences the speaker.
in al,0x61
and al,0xFC
out 0x61,al
ret

sound: ; AX = frequency Starts the speaker emiting a sound of a given frequency
mov bx,ax ; RETURNS:  AX,BX,DX = undefined
mov dx,0x12
mov ax,0x34DD ; ;mov ax,0x34DC ; which is more accurate?
div bx
mov bl,al
mov al,0xB6
out 0x43,al
mov al,bl
out 0x42,al
mov al,ah
out 0x42,al
in al,0x61
or al,3
out 0x61,al
ret

pit_tick_delay:  ; waits cx ticks, destroys: ax bx
;cli ;if you need accurate timing uncomment this
  call    read_pit_value
 mov     bx,ax
.below:
  call    read_pit_value
 cmp     ax,bx
   jl     .below
.above:
  call    read_pit_value
 cmp     ax,bx
   jg      .above
 loop    .below
;sti ;if you need accurate timing uncomment this
ret

reset_pit_timer:
 mov al,00110110b ; count down twice generator
jmp setupcommon
setup_pit_timer:  ; returns status in al
 mov al,00110100b ; rate generator
setupcommon:
cli ;if you need accurate timing comment this refer to above
 out 43h,al
 xor al,al
 out $40,al
 out $40,al
.waitlatch:
 mov al,11100010b ; get timer 0 status
  out 43h,al
  in al,40h
bt ax,6
jc .waitlatch
sti ;if you need accurate timing comment this refer to above
ret

read_pit_value:
;.waitlatch:                                  ; if you have early 8253s comment
; mov al,11100010b ; get timer 0 status       ; this part out
; out 43h,al                                  ; ( wais for latch to be valid )
; in al,40h                                   ;
;bt ax,6                                      ;
;jc .waitlatch                                ;
  mov al,11010010b
cli
  out 43h,al
  in al,40h
  mov ah,al
  in al,40h
sti
  xchg al,ah
ret

msg: db 'Playing sound',0
tonedata: ; negatives are delay
dw 440,-7,880,-4,440,-4,330,-15
dw 440,-7,880,-4,440,-4,330,-15
dw 550,-7,440,-4,330,-4,440,-15
dw 0 ; stop !

; Make the file 512 bytes long
times 512-$ Db 0 ; file is 256 bytes? FASM bug?
;times 512 DB 0    
Post 10 Jun 2007, 09:09
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 10 Jun 2007, 14:07
Quote:

; file is 256 bytes? FASM bug?


No, programmer bug. Use "times 512-$+$$ db 0" instead.
Post 10 Jun 2007, 14:07
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1905
DOS386 17 Jun 2007, 23:22
Quote:

how can I generate a tone using the PC Speaker under a 64-bit system? Do they use the same mechanism (PITs, interrupts, etc) as the 16-bit PCs, or is there a better method?


Probably not at all Shocked

Does your 64-bit have the speaker at all ? Many 32-bit P4's don't Crying or Very sad

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 17 Jun 2007, 23:22
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 18 Jun 2007, 11:31
Well, as long as you haven't enabled long mode, your 64bit x86 PC still does 16bit code... so... Smile
Post 18 Jun 2007, 11:31
View user's profile Send private message Reply with quote
Adam Kachwalla



Joined: 01 Apr 2006
Posts: 150
Adam Kachwalla 19 Jun 2007, 06:31
..


Last edited by Adam Kachwalla on 07 Mar 2013, 03:08; edited 1 time in total
Post 19 Jun 2007, 06:31
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 19 Jun 2007, 14:26
You're not in long mode in your bootsector though, are you? =)

If you understand the above code, it shouldn't be hard to use the same principle under long mode anyway.
Post 19 Jun 2007, 14:26
View user's profile Send private message Reply with quote
Adam Kachwalla



Joined: 01 Apr 2006
Posts: 150
Adam Kachwalla 19 Jun 2007, 21:33
f0dder wrote:
You're not in long mode in your bootsector though, are you? =)

If you understand the above code, it shouldn't be hard to use the same principle under long mode anyway.


No long mode in the boot sector, but I was asking whether it would be any point using similar code in a "PC speaker driver" (I'm also working on the F.S), because if there is a better alternative to run it in a 64-bit PC (at the moment emulated with Qemu). If there is an absolutely different mechanism, there is no point using the code from the boot sector!

BTW, is there any problem with USE64 in the bootsector (ie. will I run into probs in the future)? Don't seem to have problems now!


Last edited by Adam Kachwalla on 16 Oct 2008, 04:19; edited 1 time in total
Post 19 Jun 2007, 21:33
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 24 Jun 2007, 23:16
DOSBox 0.70 seems to emulate the PC speaker (via soundcard), so no worries!
Post 24 Jun 2007, 23:16
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1905
DOS386 24 Jun 2007, 23:49
> seems to emulate the PC speaker

NOT amused Sad I'd rather like to see some code to access PCI / PCIE
soundcards from DOS or "raw", without need to install and boot >=10 GiB of crap first allowing to run DOSbox at all and finally my code inside it Sad
Post 24 Jun 2007, 23:49
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 25 Jun 2007, 10:31
Then install DOS on your box and quit::whine().
Post 25 Jun 2007, 10:31
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.