Skip to content

Instantly share code, notes, and snippets.

@lukq
Created January 25, 2017 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukq/55bb161204f92aa3d4b12deb3dd793c6 to your computer and use it in GitHub Desktop.
Save lukq/55bb161204f92aa3d4b12deb3dd793c6 to your computer and use it in GitHub Desktop.
CP/M dsk generator for Sharp MZ-800
<!DOCTYPE html>
<title>Gen Dsk 800</title>
<style>
body{font:caption}
input{margin-top:.3em}
td{padding:0 .5em 0 0}
#all{color:#666}
</style>
<div style="float:left;padding:2px 3px;border:.8px solid">
Format: <select id=format onchange=listFiles()>
<option>LEC CP/M 360 kB (2s 40t 9s)</option>
<option>LEC CP/M 720 kB (2s 80t 9s)</option>
<option>LEC CP/M 1.2 MB (2s 80t 15s)</option>
<option>LEC CP/M 1.44 MB (2s 80t 18s)</option>
<option>P-CP/M80 320 kB (2s 40t 8s)</option>
</select><br>
Add files <input type=file multiple onchange=addFiles(this.files)>
<button onclick="navigator.msSaveBlob(getBlob(),'flop.dsk')">Save .dsk</button>
</div>
<table id=t style=clear:left></table>
<div id=all></div>
<script>
/*
Gen Dsk 800, version 0.3, January 2017
by Lukas Petru
Generates Sharp MZ-800 CP/M disk files.
Works in MS Edge and Chrome.
Open source under the MIT License (Expat).
Notes:
CP/M block size can be a power of two in the range 1-16 kB.
In a directory, one logical extent represents 16 kB of a file.
If the medium contains less than 256 blocks, block pointers in directory are
single-byte, otherwise they are double-byte (little-endian).
LEC CP/M formats:
360 kB: 2 sides, 40 tracks, 9 sectors, 4 system tracks, 512 B sector, 2 kB block, 4 kB directory
720 kB: 2 sides, 80 tracks, 9 sectors, 4 system tracks, 512 B sector, 2 kB block, 4 kB directory
1.2 MB: 2 sides, 80 tracks, 15 sectors, 4 system tracks, 512 B sector, 4 kB block, 8 kB directory
1.44 MB: 2 sides, 80 tracks, 18 sectors, 4 system tracks, 512 B sector, 4 kB block, 8 kB directory
P-CP/M80 format:
320 kB: 2 sides, 40 tracks, 8 sectors, 2 system tracks, 512 B sector, 2 kB block, 2 kB directory
*/
navigator.msSaveBlob?0:document.all(14).outerHTML=
`<a href=# download=flop.dsk onclick=
"href=window.URL.createObjectURL(getBlob(window.URL.revokeObjectURL(href)))"
>Save .dsk</a>`
input=[]
fileSize=x=>x+127>>7
size=x=>(fileSize(x)*10/8+.5|0)/10
extentSize=x=>x?x/16384+.99999|0:1
entrySize=x=>extentSize(x)/extent+.5|0
blockSize=x=>x/block+.9998|0
transform=x=>(x&2?155-(x>>1):x>>1)|x%2
param=[
[2048,338<<10, 1,2,128, 80,4,9, 1],
[2048,698<<10, 2,1,128,160,4,9, 1],
[4096,1160<<10,2,2,256,160,4,15,1],
[4096,1396<<10,2,2,256,160,4,18,1],
[2048,310<<10, 1,2,64, 80,2,8, 0]]
getParams=x=>[block,capacity,ptrsize,extent,dirsize,tracks,system,sectors,linear]=
param[format.selectedIndex]
listFiles=(e,q,r)=>{getParams(),alloc=e=0,files=[]
t.innerHTML=input.map(([n,s,f])=>
`<tr><td>${q=alloc+block*blockSize(s),r=e+entrySize(s),
q>capacity||r>dirsize?`<del>${n}</del>`:
(files.push([n,s,f]),alloc=q,e=r,n)}<td>${size(s)} kB`).join("")
all.innerHTML=e?`Allocated size: ${alloc/1024} kB, directory entries: ${e}`:
`No files<div style=color:#000;margin:2em;width:23em>This tool creates
virtual disk images (.dsk) in CP/M format for the Sharp MZ-800 computer.<p>
Use the file selection control at the top to add files to the disk.</div>`
}
listFiles()
fixName=x=>x.replace(/ /g,"").replace(/[^\w!-+/@\\^`{-~-]/g,"!").toUpperCase()
addFile=(f,a,b)=>{[_,a,b]=f.name.match(/([^.]*)\.?(.*)/)
a=fixName(a.slice(0,8))+"."+fixName(b.slice(0,3))
input.some(m=>m[0]==a)?exist+=", "+f.name:input.push([a,f.size,f])
}
addFiles=f=>{exist="",Array.from(f,addFile)
exist&&alert(
"Cannot add the following files as files with the same names already exist: "
+exist.slice(2))
listFiles()
}
ch=x=>String.fromCharCode(x)
// Info text, tracks, sides, track sizes
diskInfo=x=>"EXTENDED CPC DSK File\r\nDisk-Info\r\nGen Dsk 800 "+
ch(tracks>>1)+"\x02\0\0"+ch(linear?19:17)+"\x11"+"\x13".repeat(system-2)+
ch(sectors*2+1).repeat(tracks-system)+"\0".repeat(204-tracks)
// Info text, track, side, sector size, sector count
trackInfo=(i,s,w)=>"Track-Info\r\n\0\0\0\0"+ch(i>>1)+ch(i&1)+
`\0\0${ch(w=i==1?1:2)+ch(s=i==1?16:i<system&&linear?9:sectors)}\0\0`+
[...Array(s)].map((_,j)=>`\0\0${ch(j+1)+ch(w)}\0\0\0\0`).join("")+
"\0".repeat(232-8*s)
trackData=i=>i<system?"\0".repeat((i==1||system<3?8:9)*512):
data.slice((i-system)*sectors*512,(i-system+1)*sectors*512)
directory=x=>{
for(var e,j,v,r,z=[],p=dirsize*32/block,i=d=0;i<files.length;i++){
v=new Int8Array(16*entrySize(files[i][1]))
for(j=0;j<ptrsize*blockSize(files[i][1]);p++)
v[j++]=(linear?p:transform(p))&255,ptrsize>1?v[j++]=p>>8:0
for(j=0;j<entrySize(files[i][1]);j++,d++)
x=extentSize(files[i][1])-1,e=extent*(j+1)-1,x<e?e=x:0,
r=fileSize(files[i][1])-e*128,r>128?r=128:0,
z.push(files[i][0].replace(/(.*?)\.(.*)/,(_,c,d)=>
`\0${(c+" ").slice(0,8)+(d+" ").slice(0,3)+ch(e&31)}\0`+ch(e>>5))
,new Int8Array([r]),v.slice(16*j,16*j+16))
}
for(v=new Int8Array(32),v[0]=0xe5;d<dirsize;d++) z.push(v)
return z
}
getBlob=i=>{
getParams()
data=new Blob(directory().concat(
...files.map(x=>[x[2],"\0".repeat((block-x[1]%block)%block)]),
"\0".repeat(capacity-alloc+2048)))
blob=[diskInfo()]
for (i=0;i<tracks;i++) blob.push(trackInfo(i),trackData(i))
return new Blob(blob)
}
</script>
Gen Dsk 800 License
Copyright (c) 2017 Lukas Petru
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment