<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[ChipMusic.org - basic guide to programming the SID with assembly language]]></title>
	<link rel="self" href="https://chipmusic.org:80/forums/feed/atom/topic/8189/"/>
	<updated>2012-08-20T07:37:43Z</updated>
	<generator>PunBB</generator>
	<id>https://chipmusic.org/forums/topic/8189/basic-guide-to-programming-the-sid-with-assembly-language/</id>
		<entry>
			<title type="html"><![CDATA[Re: basic guide to programming the SID with assembly language]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/128003/#p128003"/>
			<content type="html"><![CDATA[<p>Nanoloop has such a different interface from a tracker that it&#039;s either really intuitive or really counterintuitive or somehow both at the same time.</p><p>I find it really easy to make something that sounds cool and have no idea how I did it or how to make anything else go with it.</p><p>And I covered a squarepusher song in Nanoloop iOS once. <img src="https://chipmusic.org/forums/img/smilies/big_smile.png" width="15" height="15" alt="big_smile" /></p><div class="quotebox"><cite>Chainsaw Police wrote:</cite><blockquote><div class="quotebox"><cite>ant1 wrote:</cite><blockquote><p>well i haven&#039;t been able to figure out nanoloop at all</p></blockquote></div><p>it&#039;s not meant to be figured out.<br />henry homesweet just uses cheat c0dez</p></blockquote></div><p>Dude we should write a GameFAQ walkthrough for nanoloop 2.x</p>]]></content>
			<author>
				<name><![CDATA[Telerophon]]></name>
				<uri>https://chipmusic.org/Telerophon</uri>
			</author>
			<updated>2012-08-20T07:37:43Z</updated>
			<id>https://chipmusic.org/forums/post/128003/#p128003</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: basic guide to programming the SID with assembly language]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/127995/#p127995"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>ant1 wrote:</cite><blockquote><p>well i haven&#039;t been able to figure out nanoloop at all</p></blockquote></div><p>it&#039;s not meant to be figured out.<br />henry homesweet just uses cheat c0dez</p>]]></content>
			<author>
				<name><![CDATA[Chainsaw Police]]></name>
				<uri>https://chipmusic.org/Chainsaw+Police</uri>
			</author>
			<updated>2012-08-20T06:47:55Z</updated>
			<id>https://chipmusic.org/forums/post/127995/#p127995</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: basic guide to programming the SID with assembly language]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/127951/#p127951"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>ant1 wrote:</cite><blockquote><p>well i haven&#039;t been able to figure out nanoloop at all</p></blockquote></div><p>No one has, some people just pretend.</p>]]></content>
			<author>
				<name><![CDATA[The Silph Scope]]></name>
				<uri>https://chipmusic.org/The+Silph+Scope</uri>
			</author>
			<updated>2012-08-20T02:51:20Z</updated>
			<id>https://chipmusic.org/forums/post/127951/#p127951</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: basic guide to programming the SID with assembly language]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/127935/#p127935"/>
			<content type="html"><![CDATA[<p>well i haven&#039;t been able to figure out nanoloop at all</p>]]></content>
			<author>
				<name><![CDATA[ant1]]></name>
				<uri>https://chipmusic.org/ant1</uri>
			</author>
			<updated>2012-08-20T00:39:48Z</updated>
			<id>https://chipmusic.org/forums/post/127935/#p127935</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: basic guide to programming the SID with assembly language]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/127934/#p127934"/>
			<content type="html"><![CDATA[<p>ant1, is there anything you <em>don&#039;t</em> know how to do?</p>]]></content>
			<author>
				<name><![CDATA[MegaMantra]]></name>
				<uri>https://chipmusic.org/MegaMantra</uri>
			</author>
			<updated>2012-08-20T00:36:05Z</updated>
			<id>https://chipmusic.org/forums/post/127934/#p127934</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: basic guide to programming the SID with assembly language]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/127850/#p127850"/>
			<content type="html"><![CDATA[<p>maybe you will be better off using a PET though: <a href="http://www.youtube.com/watch?feature=player_detailpage&amp;v=Qq4KKnYsWWo#t=37s" target="_blank">http://www.youtube.com/watch?feature=pl &#133; sWWo#t=37s</a></p>]]></content>
			<author>
				<name><![CDATA[ant1]]></name>
				<uri>https://chipmusic.org/ant1</uri>
			</author>
			<updated>2012-08-19T15:08:33Z</updated>
			<id>https://chipmusic.org/forums/post/127850/#p127850</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: basic guide to programming the SID with assembly language]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/127849/#p127849"/>
			<content type="html"><![CDATA[<p>The instruction manual had schematic diagrams in it, you can use that info to automate the toaster. I also still have a book with the room dumps disassembled and explained.</p>]]></content>
			<author>
				<name><![CDATA[chunter]]></name>
				<uri>https://chipmusic.org/chunter</uri>
			</author>
			<updated>2012-08-19T15:00:41Z</updated>
			<id>https://chipmusic.org/forums/post/127849/#p127849</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: basic guide to programming the SID with assembly language]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/127844/#p127844"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>The Silph Scope wrote:</cite><blockquote><p>Make a guide on how to make the C64 make toast.</p></blockquote></div><p>I&#039;m pretty sure you could rip power of the PSU and plug in a toaster. Not sure though....</p>]]></content>
			<author>
				<name><![CDATA[dsv101]]></name>
				<uri>https://chipmusic.org/dsv101</uri>
			</author>
			<updated>2012-08-19T13:45:27Z</updated>
			<id>https://chipmusic.org/forums/post/127844/#p127844</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: basic guide to programming the SID with assembly language]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/127818/#p127818"/>
			<content type="html"><![CDATA[<p>Make a guide on how to make the C64 make toast.</p>]]></content>
			<author>
				<name><![CDATA[The Silph Scope]]></name>
				<uri>https://chipmusic.org/The+Silph+Scope</uri>
			</author>
			<updated>2012-08-19T06:26:12Z</updated>
			<id>https://chipmusic.org/forums/post/127818/#p127818</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: basic guide to programming the SID with assembly language]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/127814/#p127814"/>
			<content type="html"><![CDATA[<p>couple of months <img src="https://chipmusic.org/forums/img/smilies/smile.png" width="15" height="15" alt="smile" />~ that&#039;s why i&#039;m writing all these tutorials (teaching is the best way of learning etc)</p>]]></content>
			<author>
				<name><![CDATA[ant1]]></name>
				<uri>https://chipmusic.org/ant1</uri>
			</author>
			<updated>2012-08-19T06:01:52Z</updated>
			<id>https://chipmusic.org/forums/post/127814/#p127814</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: basic guide to programming the SID with assembly language]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/127813/#p127813"/>
			<content type="html"><![CDATA[<p>jesus, how long have you been working with commodore shit</p>]]></content>
			<author>
				<name><![CDATA[Aeros]]></name>
				<uri>https://chipmusic.org/Aeros</uri>
			</author>
			<updated>2012-08-19T06:00:41Z</updated>
			<id>https://chipmusic.org/forums/post/127813/#p127813</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[basic guide to programming the SID with assembly language]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/127810/#p127810"/>
			<content type="html"><![CDATA[<p>hi, if anyone is interested in writing a c64 music engine</p><p>i will get you started</p><p>enjoy!</p><div class="codebox"><pre><code>/*
ant1&#039;s guide to programming the SID  
this is kick assembler source-code
made in 2012
*/

.pc=$801
:BasicUpstart($810)
.pc=$810

  // clear all sid registers to 0
  ldx #$00
  lda #$00
clearsidloop:
  // SID registers start at $d400
  sta $d400
  inc clearsidloop+1 
  inx
  cpx #$29 // and there are 29 of them
  bne clearsidloop

  // set master volume and turn filter off
  lda #%00001111 
  sta filtermode_volume

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

mainloop:

  // check if rasterline is #$ff 
  // which happens every 20ms
  // do nothing and jump back to mainloop if not
  lda $d012 
  cmp #$ff
  bne mainloop

  // play a single frame of music (if there are any notes to play this frame)
  jsr music

  // loop forever
  jmp mainloop

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

music:

  // tickcounter counts the number of 20ms frames
  inc tickcounter

  // speed is the number of 20ms frames that will happen before the slowtable advances one position
  lda tickcounter
  cmp speed
  beq playnote
  jmp endmusic
  
playnote:

  // flash border and background
  inc $d020
  dec $d021
  
  // reset gatebit/waveform
  lda #%00000000
  sta $d404

  // reset tickcounter as we have got to the next row of the slowtable and we want to start counting towards the next note
  lda #$00
  sta tickcounter
  
  // add slowtablepos to slowtable to get the current position in the slowtable
  ldx slowtablepos
  lda slowtable, x
  
  // get low byte of frequency from slowtable
  sta frequency
  
  // get high byte of frequency from slowtable
  inx
  lda slowtable, x
  sta frequency+1
  
freqcheck1:

  // check if low byte of frequency is #$00, if not, continue to play note
  lda frequency
  cmp #$00
  bne playnote2
  
  // check if high byte of frequency is #$00 (frequency is #$0000), if not, continue to next check
  lda frequency+1
  cmp #$00
  bne freqcheck2
  
  // if it is, reset slowtablepos and start again
  lda #$00
  sta slowtablepos
  jmp playnote  
  
freqcheck2:

  // check if high byte of frequency is #$01 (frequency is #$0001), if not continue to play note
  lda frequency+1
  cmp #$01
  bne playnote2
  
  // if it is, increment slowtablepos to continue moving through the table, but jump back to the start without playing a note
  inc slowtablepos
  inc slowtablepos
  jmp music
    
playnote2:

  // set attack to 0, decay to 6
  lda #$06
  sta attack_decay
  
  // set sustain and release to 0
  lda #$00
  sta sustain_release
  
  // move frequency from frequency variable to sid buffer
  lda frequency
  sta freq_lowbyte
  lda frequency+1
  sta freq_highbyte
  
  // set waveform to saw and turn gatebit on
  lda #%00100001
  sta control

  // increment the slowtablepos twice (twice because there are two bytes for each note in the table)
  inc slowtablepos
  inc slowtablepos

  // write data from the sid buffer to the sid chip
  lda freq_lowbyte
  sta $d400
  lda freq_highbyte
  sta $d401
  lda control
  sta $d404
  lda attack_decay
  sta $d405
  lda sustain_release
  sta $d406
  lda filtermode_volume
  sta $d418

endmusic:

  // back to mainloop
  rts


// variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// the number of frames to wait before advancing the slowtable, you can set this to anything
speed:
  .byte $1f

// stores the tick (frame) counter
tickcounter:
  .byte $00

// stores the position in the slowtable  
slowtablepos:
  .byte $00

// temporarily stores the frequency of the next note  
frequency:
  .byte $00
  .byte $00

// music data ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// the slowtable is where the note data is stored
slowtable:

  // frequency is a sixteen bit number, a higher number is a higher pitch
  .byte $b1, $25
  .byte $30, $40
  .byte $b1, $25
  
  // a frequency of #$0001 tells the player to play no note on this row
  .byte $00, $01
  
  .byte $b1, $25
  .byte $30, $40
  .byte $b1, $15
  .byte $00, $01
  
  .byte $b1, $25
  .byte $30, $40
  .byte $b1, $25
  .byte $00, $01
  
  .byte $b1, $25
  .byte $30, $40
  .byte $b1, $65
  .byte $00, $01
  
  // a frequency of #$0000 tells the player to loop back to the start of the slowtable
  .byte $00, $00
  
// sid buffer ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// values that are going to be written to the sid are stored here and then written in one go

// freq_lowbyte is the low byte of the frequency
// on the SID this is $d400 
freq_lowbyte:
  .byte $00
  
// freq_highbyte is the high byte of the frequency
// on the SID this is $d401
freq_highbyte:  
  .byte $00

// control sets the waveform and gatebit
// on the SID this is $d404
control:
  .byte $00
  
// the first nybble of attack_decay is attack, the second nybble is decay
// on the SID this is $d405
attack_decay:
  .byte $00
  
// the first nybble of sustain_release is sustain, the second nybble is release
// on the SID this is $d406
sustain_release:
  .byte $00
  
// bits 7-4 of filtermode_volume control the filter, bits 3-1 control the master volume
// on the SID this is $d418
filtermode_volume:
  .byte $00</code></pre></div><p>edit:<br />if you&#039;ve never done any c64 programming before then you will need, to follow this guide,</p><p><a href="http://aanaaanaaanaaana.net/c64-docs-archive/6502%20Reference.htm" target="_blank">something that tells you what all the opcodes do</a><br /><a href="http://aanaaanaaanaaana.net/c64-docs-archive/All%20About%20Your%20C64.zip" target="_blank">something that tells you what all the sid registers do</a><br /><a href="http://aanaaanaaanaaana.net/tools-archive/kickassembler.zip" target="_blank">kick assembler</a></p>]]></content>
			<author>
				<name><![CDATA[ant1]]></name>
				<uri>https://chipmusic.org/ant1</uri>
			</author>
			<updated>2012-08-19T05:36:58Z</updated>
			<id>https://chipmusic.org/forums/post/127810/#p127810</id>
		</entry>
</feed>
