DISK.VHPOS.ASM 34.4 KB
Newer Older
ASCP's avatar
ASCP committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
*************************************************************************
* DISK-2-DISK Commodore 64/128 to AmigaDOS file conversion utility.	*
* Copyright (c) 1987 Central Coast Software				*
* All rights reserved							*
* George Chamberlain							*
* January 20, 1987							*
*************************************************************************

	INCLUDE "D2DMAC.ASM"

	XDEF	READ_SECTOR,WRITE_SECTOR,FORMAT_TRACK
	XDEF	DISK_INIT,DISK_EXIT,DISK_CAL,MOTOR_OFF
	XDEF	INIT_FORMAT,STORE_D0_D1_TIMES
	XDEF	SETUP_TRACK,MON,CLR_TPARAM,CHECK_PROTECTION

	XDEF	NEW_SIDE,NEW_TRACK,DR_BASE,DRIVE_SEL,SEC_PER_TRACK
	XDEF	MSG_PORT

	XREF	DISK_ID,SysBase,DISP_ERR
	XREF	BAMBUF,BLKBUF,BLKCNT,VALID_BAM

* HARDWARE REGISTER DEFINITIONS

DSKPTR	EQU	$DFF020
DSKLEN	EQU	$DFF024
DSKBYTR	EQU	$DFF01A
DSKSYNC	EQU	$DFF07E
CIAA	EQU	$BFE001
CIAB	EQU	$BFD100
INTENAR	EQU	$DFF01C
INTENA	EQU	$DFF09A
INTREQ	EQU	$DFF09C
INTREQR	EQU	$DFF01E
ADKCON	EQU	$DFF09E
ADKCONR	EQU	$DFF010
VHPOSR	EQU	$DFF006

TRKSIZ	EQU	10000		;TRACK BUFFR SIZE IN BYTES
SECSIZ	EQU	256		;NUMBER OF BYTES IN A SECTOR (BLOCK)
SECMAX	EQU	21		;MAX NUMBER OF SECTORS
FILCNT	EQU	8000		;MAX NUMBER OF 55'S TO STORE IN TRACK_BUF
SECLEN	EQU	336/2		;NUMBER OF WORDS IN NORMAL SECTOR WRITE

* READ SECTOR: TRACK IN D1 (1 0RIGIN), SECTOR IN D0 (0 ORIGIN), BUFFER IN A0.

READ_SECTOR:
	CLR.B	DISK_MODE	;READ MODE
        BSR	SETUP_TRACK	;CALCULATE PHYSICAL TRACK, SIDE, SECTOR
	BSR	BUILD_RW_HDR
	MOVE.B  #10,RETRY_COUNT
1$:	BSR	SELECT_DRIVE    ;GET DRIVE READY
        BSR	SEEK            ;STEP HEADS, IF NECESSARY
	BSR	DISK_READ	;READ THE SECTOR
	ERROR	2$              ;COULDN'T FILE HEADER
	BSR	DECODE_SECTOR	;CONVERT DATA AND MOVE TO USERS BUFFER
	NOERROR	9$
2$:	MOVE.L	DUTY_PTR,A0	;FORCE SPEED ADJUSTMENT, JUST IN CASE
	CLR.W	(A0)		;ZAP STORED TRACK SPEED SETTING
	CLR.W	2(A0)
	DECB    RETRY_COUNT     ;SECTOR TABLE ERROR
	BNE.S	1$		;TRY AGAIN
	DispErr	READ_ERR.       ;C/C SECTOR READ ERROR
	STC                     ;RETURN ERROR FLAG
9$:	RTS

* WRITE SECTOR: TRACK IN D1 (1 0RIGIN), SECTOR IN D0 (0 ORIGIN), BUFFER IN A0.

WRITE_SECTOR:
	MOVE.B	D1,DISK_MODE	;>0=WRITE MODE
        BSR	SETUP_TRACK	;CALCULATE PHYSICAL TRACK, SIDE, SECTOR
	BSR	BUILD_RW_HDR
;	BSR	CHECK_PROTECTION ;IS DISK WRITE_PROTECTED?
	ERROR   9$              ;IT IS...DONT PROCEED
	BSR	ENCODE_SECTOR
	MOVE.W	#SECLEN,WRTLEN	;NORMAL DMA WRITE
	MOVE.L	TRACK_BUF,WRTBUF
	MOVE.B  #5,RETRY_COUNT
1$:	BSR	SELECT_DRIVE    ;GET DRIVE READY
	BSR	SEEK            ;STEP HEADS, IF NECESSARY
	BSR	DISK_WRITE	;WRITE THE SECTOR
	ERROR   2$              ;SOMETHING IS WRONG WITH DISK
	RTS                     ;BACK TO CALLER...ALL IS WELL

2$:	MOVE.L	DUTY_PTR,A0	;FORCE SPEED ADJUSTMENT, JUST IN CASE
	CLR.W	(A0)		;ZAP STORED TRACK SPEED SETTING
	CLR.W	2(A0)
	DECB    RETRY_COUNT     ;SECTOR TABLE ERROR
	BNE.S	1$
	DispErr	READ_ERR.	;THIS SHOULD BE WRITE ERROR
9$:	STC                     ;RETURN ERROR FLAG
	RTS

* DEBUGGING - SPEED ADJUSTMENT SENSING

TEST_SECTOR:
	BSR	MOTOR_ON
	MOVE.B	#10,COUNT
	CLR.B	SPDERR
	CLR.B	DTAERR
	CLR.B	HDRERR
1$:	MOVEQ	#1,D1		;TRACK
	MOVEQ	#6,D0		;SECTOR
	LEA	BLKBUF,A0	;READ BUFFER
	CLR.B	DISK_MODE	;READ MODE
        BSR	SETUP_TRACK	;CALCULATE PHYSICAL TRACK, SIDE, SECTOR
	MOVE.W	TRACK_OFF,OFF_DUTY
	MOVE.W	TRACK_ON,ON_DUTY
	BSR	BUILD_RW_HDR
	BSR	SELECT_DRIVE    ;GET DRIVE READY
        BSR	SEEK            ;STEP HEADS, IF NECESSARY
	MOVE.W	#$1700,ADKCON
	BSR	CALC_AVG_SPEED
	NOERROR	2$              ;COULDN'T FILE HEADER
	INCB	SPDERR
	BRA	9$
2$:	MOVE.L	D0,AVGSPD
	BSR	READ_HEADER	;GET READY TO READ
	NOERROR	3$
	INCB	HDRERR
	BRA	9$
3$:	TST.B	FULL_SPEED
	BNE.S	14$			;DONT TURN OFF DRIVE FOR FULL SPEED
	BSET	#7,D2			;TURN MOTOR OFF
	BSET	#7,D3
	MOVE.B	D2,(A1)			;DESELECT DRIVE, SET MOTOR BIT
	MOVE.B	D3,(A1)			;RESELECT WITH PROPER MOTOR BIT
14$:	MOVE.W	#$FFEA,DSKSYNC		;SYNC ON END OF SYNC PATTERN
	MOVE.W	#$3002,INTREQ
	MOVE.W	#$8400,ADKCON		;ENABLE DMA SYNC
	MOVE.L	TRACK_BUF,DSKPTR	;READ INTO TRACK BUFFER
	MOVE.W	#$80A3,D0		;326 GCR BYTES
	MOVE.W	D0,DSKLEN		;START DMA TRANSFER INTO BUFFER
	MOVE.W	D0,DSKLEN
;	ENABLE
7$:	MOVE.W	INTREQR,D0		;GOT SECTOR?
	BTST	#1,D0
	BEQ.S	7$			;NO
	MOVE.W	#$4000,DSKLEN		;STOP DMA
	MOVE.W	#$3002,INTREQ
	ENABLE
	MOVE.B  #$7F,CIAB		;CLEAR MOTOR-OFF BIT
	MOVE.B	SAVE_D0,CIAB		;SELECT DRIVE TO TURN MOTOR BACK ON
	BSR	DECODE_SECTOR		;CONVERT DATA AND MOVE TO USERS BUFFER
	NOERROR	9$
	INCB	DTAERR
9$:	BSR	FREE_DRIVES
	DECB	COUNT
	BNE	1$
	BSR	MOTOR_OFF
	JMP	TEST_SECTOR

AVGSPD	DC.L	0
HDRERR	DC.B	0
SPDERR	DC.B	0
DTAERR	DC.B	0
COUNT	DC.B	0

* FINDS PROPER HEADER ON DISK, THEN READS SECTOR FOLLOWING INTO SECTOR
* BUFFER.  ENCODED SECTOR IN D5 AND D6.

DISK_READ:
        MOVE.W	#$1700,ADKCON
	BSR	ADJUST_DUTY_CYCLE	;ADJUST TRACK SPEED, IF NECESSARY
	ERROR	6$			;READ PROBLEM
	BSR	READ_HEADER
	NOERROR	5$			;GOT THE HEADER
6$:	ENABLE
	BSR	FREE_DRIVES
	STC
	RTS
5$:	TST.B	FULL_SPEED
	BNE.S	14$			;DONT TURN OFF DRIVE FOR FULL SPEED
	BSET	#7,D2			;TURN MOTOR OFF
	BSET	#7,D3
	MOVE.B	D2,(A1)			;DESELECT DRIVE, SET MOTOR BIT
	MOVE.B	D3,(A1)			;RESELECT WITH PROPER MOTOR BIT
14$:	MOVE.W	#$FFEA,DSKSYNC		;SYNC ON END OF SYNC PATTERN
	MOVE.W	#$3002,INTREQ
	MOVE.W	#$8400,ADKCON		;ENABLE DMA SYNC
	MOVE.L	TRACK_BUF,DSKPTR	;READ INTO TRACK BUFFER
	MOVE.W	#$80A3,D0		;326 GCR BYTES
	MOVE.W	D0,DSKLEN		;START DMA TRANSFER INTO BUFFER
	MOVE.W	D0,DSKLEN
;	ENABLE
7$:	MOVE.W	INTREQR,D0		;GOT SECTOR?
	BTST	#1,D0
	BEQ.S	7$			;NO
	MOVE.W	#$4000,DSKLEN		;STOP DMA
	MOVE.W	#$3002,INTREQ
	MOVE.B  #$7F,CIAB		;CLEAR MOTOR-OFF BIT
	MOVE.B	SAVE_D0,CIAB		;SELECT DRIVE TO TURN MOTOR BACK ON
	ENABLE
	BSR	FREE_DRIVES
	RTS

* FINDS PROPER HEADER ON DISK, THEN WRITES ENCODED DATA IN TRACK BUFFER
* TO DISK.  ENCODED SECTOR IN D5 AND D6.

DISK_WRITE:
	MOVE.W	#$1700,ADKCON
;	MOVE.W	#$7700,ADKCON
	MOVE.W	#$C000,ADKCON		;280 NS PRECOMP
	BSR	ADJUST_DUTY_CYCLE	;ADJUST TRACK SPEED, IF NECESSARY
	ERROR	6$
	BSR	READ_HEADER
	NOERROR	5$			;GOT THE HEADER
6$:	ENABLE
	BSR	FREE_DRIVES
	STC
	RTS
5$:	TST.B	FULL_SPEED
	BNE.S	14$			;DONT TURN OFF DRIVE FOR FULL SPEED
	BSET	#7,D2			;TURN MOTOR OFF
	BSET	#7,D3
	MOVE.B	D2,(A1)			;DESELECT DRIVE, SET MOTOR BIT
	MOVE.B	D3,(A1)			;RESELECT WITH PROPER MOTOR BIT
14$:	MOVE.W	#$3002,INTREQ
	MOVE.L	WRTBUF,DSKPTR		;NOW COPY FULL DATA SECTOR
	MOVE.W	WRTLEN,D0		;NUMBER OF WORDS
	OR.W	#$C000,D0		;SET DMA ON AND WRITE
	MOVE.W	D0,DSKLEN		;NOW START TRANSFER INTO BUFFER
	MOVE.W	D0,DSKLEN
;	ENABLE
7$:	MOVE.W	INTREQR,D0		;GOT SECTOR?
	BTST	#1,D0
	BEQ.S	7$			;NO
	MOVE.W	#$4000,DSKLEN		;STOP DMA
	MOVE.W	#$3002,INTREQ
	MOVE.B  #$7F,CIAB		;CLEAR MOTOR-OFF BIT
	MOVE.B	SAVE_D0,CIAB		;SELECT DRIVE TO TURN MOTOR BACK ON
	ENABLE
	BSR	FREE_DRIVES
	RTS

* FINDS PROPER HEADER ON TRACK.  RETURNS CY=1 IF NOT FOUND.
* D0 CONTAINS TYPE OF OPERATION: 0=READ, 1=WRITE.

RHLIMIT	EQU	4720	;APPROX NUMBER OF 63.5 USEC SCAN LINES IN 300 MS

READ_HEADER:
	DISABLE				;NO INTERRUPTS HERE
	MOVE.W	#$3002,INTREQ		;JUST IN CASE...
	BSR	SLOW			;SLOW THE DRIVE TO APPROX SPEED
	LEA	DSKBYTR,A0		;READ A BYTE HERE
	LEA	CIAB,A1
	MOVE.W	ON_DUTY,D4		;LOAD DUTY CYCLE COUNTS
	SWAP	D4
	MOVE.W	OFF_DUTY,D4		;START WITH MOTOR OFF
	MOVE.W	#RHLIMIT,A3		;MAX HEADER SCAN TICKS
	LEA	VHPOSR,A2
	MOVE.W	(A2),A5			;GET INITIAL SETTING
	MOVEQ	#-1,D2			;DESELECT CONSTANT
	MOVE.B	SAVE_D0,D3		;DRIVE SELECT VALUE WITH SIDE
	BSET	#7,D3			;WITH MOTOR OFF BIT
	MOVE.L	SECTOR_BUF,DSKPTR	;SET A DMA ADDRESS, JUST IN CASE...
	MOVE.W	#0,DSKLEN		;START THE DISK READING
	ZAP	D1			;MOTOR CYCLE COUNTER
	ZAP	D7			;MISSED HEADER COUNTER
	IFNZ	FULL_SPEED,RHLOOP	;DONT TURN OFF MOTOR
	MOVE.B	D2,(A1)			;ELSE TURN IT OFF
	MOVE.B	D3,(A1)

RHLOOP:	BSR.S	CHECK_MOTOR		;REV MOTOR, IF NECESSARY
	BSR.S	WAIT_CHAR		;GET A CHAR
	CMP.B	#-1,D0			;-1?
	BNE.S	RHLOOP
	BSR.S	WAIT_CHAR		;GET NEXT CHAR
	CMP.B	#-1,D0			;GOT A SYNC PATTERN?
	BNE.S	RHLOOP
	BRA.S	RHSYNC			;YES...CHECK OUT POSSIBLE HEADER

WAIT_CHAR:
	MOVE.W	(A0),D0			;GOT A CHAR?
	BPL.S	1$			;NOT YET
	RTS				;ELSE RETURN WITH CHAR IN D0
1$:	MOVE.W	(A2),D0			;GET CURRENT VPOS
	AND.W	#$FF00,D0
	CMP.W	A5,D0			;SAME AS LAST TIME?
	BEQ.S	WAIT_CHAR		;STILL THE SAME VALUE
	MOVEA.W	D0,A5			;SAVE NEW VALUE
	INCW	D7			;NO...COUNT IT
	INCW	D1			;ALSO FOR LOOP
	BRA.S	WAIT_CHAR

CHECK_MOTOR:
	CMP.W	D1,D4			;NEED TO REVERSE MOTOR?
	BLS.S	1$			;YES
	RTS				;ELSE RETURN
1$:	CMP.W	A3,D7			;HAVE WE ALSO MISSED THE HEADER?
	BCS.S	2$			;NOT YET...REVERSE THE MOTOR
	MOVE.W	#$4000,DSKLEN		;ELSE BAIL OUT, CY=1
	BCLR	#7,D2			;TURN MOTOR ON
	BCLR	#7,D3
	MOVE.B	D2,(A1)			;DESELECT DRIVE, MOTOR ON
	MOVE.B	D3,(A1)			;RESELECT DRIVE, MOTOR ON
	ADDQ.L	#4,A7			;CLEAN UP STACK
	STC
	RTS

2$:	TST.B	FULL_SPEED
	BNE.S	3$
	BCHG	#7,D2
	BCHG	#7,D3
	MOVE.B	D2,(A1)			;DESELECT DRIVE, SET MOTOR BIT
	MOVE.B	D3,(A1)			;RESELECT WITH PROPER MOTOR BIT
3$:	SWAP	D4
	ZAP	D1			;START WITH EMPTY MOTOR COUNTER
	RTS	

RHSYNC:	LEA	HDRBUF,A4		;HEADER GOES HERE
	MOVEQ	#9,D5			;10 BYTES IN HEADER
7$:	BSR.S	WAIT_CHAR		;GET NEXT CHAR
	CMP.B	#-1,D0			;STILL IN SYNC PATTERN?
	BEQ.S	7$			;YES
	BRA.S	9$			;NO...STORE IT

8$:	BSR.S	WAIT_CHAR		;GET ANOTHER HEADER CHAR
9$:	MOVE.B	D0,(A4)+		;NO...STORE THE HEADER BYTE
	DBF	D5,8$			;STORE ALL OF HEADER
	MOVE.L	HDRBUF+4,D6		;NORMALIZE THE HEADER DATA
	MOVE.L	HDRBUF,D5
	BRA.S	11$
10$:	ADD.L	D6,D6			;SHIFT AND SET X
	ADDX.L	D5,D5			;COMPLETE THE LEFT SHIFT
11$:	BMI.S	10$			;SHIFT UNTIL "NORMALIZED"
	AND.L	HMASK0,D5
	CMP.L	NEWHDR,D5		;CHECK WITH THE DESIRED HDR
	BNE	RHLOOP			;NO MATCH...KEEP ON TRUCKIN'
	AND.L	HMASK1,D6
	CMP.L	NEWHDR+4,D6
	BNE	RHLOOP			;NO MATCH
	MOVE.W	#$4000,DSKLEN		;FOUND HEADER...STOP ANY MORE DMA
	RTS

* MEASURES DISK SPEED AND ADJUSTS TRACK DUTY CYCLE, IF NECESSARY.
* IF TRACK HAS BEEN ADJUSTED SINCE CALIBRATION, USES PRESERVED VALUE.

DUTY_DELT EQU	4			;DUTY CYCLE ADJUSTMENT
MAXERR	  EQU	100			;ERROR TOLERANCE

ADJUST_DUTY_CYCLE:
	IFZ	FULL_SPEED,1$		;NEED TO ADJUST
	MOVE.W	#184,D0			;ELSE USE STANDARD VALUE
	MOVE.W	D0,OFF_DUTY
	MOVE.W	D0,ON_DUTY
	BRA	8$
1$:	MOVE.L	DUTY_PTR,A0
	MOVE.W	2(A0),ON_DUTY
	MOVE.W	(A0),OFF_DUTY		;GET STORED DUTY CYCLE, IF ANY
	BNE	8$			;ALREADY ADJUSTED...DONT CHANGE
	MOVE.W	TRACK_OFF,OFF_DUTY	;ELSE USE TPARAM VALUE FOR THIS TRACK
	MOVE.W	TRACK_ON,ON_DUTY
	LEA	ADJTBL,A0
	MOVE.L	A0,ADJPTR		;RESET PTR TO SPEED CORRECTION TABLE
2$:	BSR	CALC_AVG_SPEED		;MEASURE CURRENT SPEED
	NOERROR	4$
3$:	MOVE.W	TRACK_OFF,OFF_DUTY	;HEADER ERROR...RESET TO INITIAL VALUE
	MOVE.W	TRACK_ON,ON_DUTY
	MOVE.L	ADJPTR,A0
	MOVE.W	(A0)+,D2		;GET A SPEED ADJUSTMENT FROM TABLE
	BEQ.S	9$			;OOPS...THAT'S THE END, GIVE UP
	MOVE.L	A0,ADJPTR
	BRA.S	6$			;ELSE MAKE A BIG CHANGE IN SETTING	
4$:	MOVEQ	#DUTY_DELT,D2		;DUTY CYCLE DELTA
	ZAP	D1
	MOVE.W	TRACK_SPEED,D1		;GET TARGET SPEED
	MOVE.L	#MAXERR,D7		;RANGE LIMIT
	CMP.L	D1,D0			;OFF WHICH WAY?
	BPL.S	5$			;SPEED SLOWER THAN SPEC (TICKS HIGH)
	EXG	D1,D0			;ELSE SWAP REGS
	NEG.W	D2			;AND REVERSE ADJUSTMENT
5$:	SUB.L	D1,D0			;CHECK DIFFERENCE
	CMP.L	D7,D0			;OUT OF RANGE?
	BLS.S	7$			;WITHIN TOLERANCE
	ADD.L	D7,D7
	CMP.L	D7,D0			;WAY OFF?
	BLS.S	6$			;NO
	ADD.L	D2,D2			;ELSE DOUBLE THE ADJUSTMENT
6$:	ADD.W	D2,ON_DUTY
	BMI.S	3$			;TOO FAR
	SUB.W	D2,OFF_DUTY		;ADJUST DUTY CYCLE BY DELTA AMOUNT
	BMI.S	3$			;OOPS...TOO FAR...JUST GIVE UP
	BRA.S	2$			;AND TRY AGAIN
7$:	MOVE.L	DUTY_PTR,A0
	MOVE.W	OFF_DUTY,(A0)		;STORE TEMP VALUES FOR THIS GROUP
	MOVE.W	ON_DUTY,2(A0)
8$:	RTS
9$:	STC
	RTS

* CALCULATES THE AVERAGE SPEED OF THE DISK USING CURRENT SETTINGS, AND 
* RETURNS THE AVERAGE SPEED VALUE IN D0.

CALC_AVG_SPEED:
	CLR.L	SPEED_AVG		;FOR AVERAGING SPEED
	MOVEQ	#3,D1			;4 TRIES
1$:	PUSH	D1
	BSR	MEASURE_SPEED		;GET ONE MEASUREMENT
	POP	D1
	ERROR	9$			;DISK NOT WORKING
	ADD.L	D0,SPEED_AVG		;ADD IN THIS MEASUREMENT
	DBF	D1,1$			;LOOP
	MOVE.L	SPEED_AVG,D0		;GET RESULT
	LSR.L	#2,D0			;DIVIDE BY 4
	MOVE.L	D0,SPEED_AVG		;THIS IS WHAT WE MEASURED
9$:	RTS

* MEASURESS TRACK SPEED FACTOR IN D0 BY MEASURING THE TIME BETWEEN
* SUCCESSIVE OCCURRENCES OF THE REQUESTED SECTOR HEADER.  FAILS IF SECTOR 
* CANNOT BE FOUND.

MEASURE_SPEED:
	BSR	READ_HEADER		;FIND HEADER OF THIS SECTOR
	ERROR	9$
	ZAP	D7
	MOVE.W	#0,DSKLEN		;RESTART THE DISK
	BSR	RHLOOP			;NOW LOOK FOR SAME HEADER AGAIN
	ERROR	9$
	BCLR	#7,D2			;MAKE SURE MOTOR IS ON
	BCLR	#7,D3
	MOVE.B	D2,(A1)
	MOVE.B	D3,(A1)
	MOVE.L	D7,SPEED
	MOVE.L	D7,D0
9$:;	ENABLE				;TURNED OFF IN READ_HEADER
	RTS

* ADJUST DRIVE SPEED AND HOLD FOR A WHILE

SLOW:
	IFNZ	FULL_SPEED,9$		;FULL SPEED...NO DELAY
	MOVE.W	DELAY_COUNT,D6		;SPEED CONTROL LOOP COUNT
	BSR	DOFF			;ADJUST MOTOR-ON DUTY CYCLE
	BSR	DOFF			;DOUBLE INITIAL SLOW DOWN
2$:	BSR	DOFF
	BSR	DON
	DBF	D6,2$
;	MOVE.B  #$7F,CIAB		;CLEAR MOTOR-OFF BIT
;	MOVE.B	SAVE_D0,CIAB		;SELECT DRIVE TO TURN MOTOR BACK ON
;	MOVE.B	#-1,CIAB
;	MOVE.B	SAVE_D0,D0
;	BSET	#7,D0
;	MOVE.B	D0,CIAB			;TURN MOTOR OFF
9$:	RTS

* MOTOR SPEED CONTROL ROUTINES

DON:	MOVE.W  ON_DUTY,D1		;MAKE SURE IT STAYS ON A WHILE
	BEQ.S	1$
	MOVE.B  #$7F,CIAB		;MOTOR ON
	MOVE.B  SAVE_D0,CIAB
	BSR	SDELAY
1$:	RTS

DOFF:	MOVE.W	OFF_DUTY,D1		;LEAVE IT OFF FOR A WHILE
	BEQ.S	1$
	MOVE.B	SAVE_D0,D0
	BSET	#7,D0
	MOVE.B	#-1,CIAB		;SET MOTOR-OFF BIT
	MOVE.B  D0,CIAB			;SELECT DRIVE TO TURN OFF MOTOR
	BSR	SDELAY
1$:	RTS

* MOTOR SPEED CONTROL DUTY CYCLE DELAY COUNTER.

SDELAY:
	PUSH	D1
	POP	D1
	DBF	D1,SDELAY
	RTS

* CHECKS SELECTED DRIVE FOR WRITE-PROTECTION STATUS, ALERTS
* OPERATOR AND SETS CY IF SO.

CHECK_PROTECTION:
	BSR	BUSY_DRIVES	;GET CONTROL
	MOVE.B	#-1,CIAB
	MOVE.B	DRIVE_SEL,CIAB	;SELECT DRIVE
	MOVE.B	CIAA,D0
	PUSH	D0
	BSR	FREE_DRIVES
	POP	D0
	BTST    #3,D0		;PROTECTION ON?
        BNE     1$              ;NO
	DispErr	PROT.           ;WARN OPERATOR
        STC
1$:	RTS

* SAVES TRACK AND SECTOR, CALCULATES SIDE, AND SETS UP TRACK PARAMETERS.
* TRACK IN D1, SECTOR IN D0, BUFFER IN A0.

SETUP_TRACK:
	MOVE.L	A0,DMAADR	;SAVE BUFFER ADDRESS
	MOVE.B  D0,NEW_SECTOR   ;SAVE SECTOR
	MOVE.B	D1,ACTUAL_TRACK	;SAVE TRACK BEFORE ADJUSTMENT
	MOVE.B	#4,NEW_SIDE	;ASSUME SIDE 0
	CMP.B	#36,D1		;SIDE 0?
	BCS.S	1$		;YES
	CLR.B	NEW_SIDE	;NO...SIDE 1
	SUB.W	#35,D1		;
1$:	DECW	D1		;ADJUST TRACK TO 0 ORIGIN
        MOVE.B  D1,NEW_TRACK    ;SAVE NEW PHYSICAL TRACK
	MOVEQ	#TPAR_LEN,D0	;FOR PARAM TABLE INDEXING
	LEA	TPARAM_TBL,A0	;BASE OF TRACK PARAM TABLE
	CLR.B	FULL_SPEED	;NOT FULL SPEED...YET
	CMP.B	#17,D1		;TRACK 1-17 (0-16)?
	BCS.S	2$		;YES
	ADD.L	D0,A0
	TST.B	DISK_MODE	;READING?
	BNE.S	5$		;YES
	MOVE.B	D1,FULL_SPEED	;NO...RUN AT FULL SPEED
5$:	CMP.B	#24,D1		;TRACK 18-24(17-23)?
	BCS.S	2$		;YES
	ADD.L	D0,A0
	CMP.B	#30,D1		;TRACK 25-30(24,29)?
	BCS.S	2$		;YES
	ADD.L	D0,A0		;ELSE MUST BE TRACK 31-35(30-34)
	MOVE.B	D1,FULL_SPEED	;LAST TRACKS ALWAYS FULL SPEED
2$:	MOVE.W	(A0)+,SEC_PER_TRACK ;NUMBER OF SECTORS ON THIS TRACK
	MOVE.W	(A0)+,TRACK_SIZE ;NUMBER OF WORDS PER TRACK
	MOVE.W	(A0)+,GAP_COUNT	;FILL BYTES BETWEEN SECTORS
	MOVE.W	(A0)+,TRACK_SPEED ;TARGET TRACK SPEED 
	MOVE.W	(A0)+,TRACK_OFF ;INITIAL OFF_DUTY
	MOVE.W	(A0)+,TRACK_ON	;INITIAL ON_DUTY
	MOVE.L	A0,DUTY_PTR	;POINT TO DUTY CYCLE PARAMS
	MOVE.W	#20,DELAY_COUNT	;SPEED STABILIZING DELAY
	IFZ	DISK_MODE,9$	;READ
	MOVE.W	#40,DELAY_COUNT ;ELSE GET SPEED VERY ACCURATE
9$:	RTS

* SELECT DRIVE, AND CALIBRATE TO TRACK 0.

HOME_DISK:
        BSR	BUSY_DRIVES
        MOVE.B  DRIVE_SEL,D0            ;DRIVE SELECT CODE WITH MOTOR ON
        BSET    #2,D0                   ;SIDE 0
        BCLR    #1,D0                   ;TOWARD SPINDLE
        MOVE.B  D0,CIAB
        MOVE.B  D0,SAVE_D0              ;SAVE VALUE
        MOVEQ   #2,D2                   ;MAX STEPS TO TRACK 0
        DISABLE
1$:;     BTST    #4,CIAA                 ;ON TRACK 0?
;        BNE.S   2$                      ;NO...NO NEED TO GO FURTHER
        BSR	STEP                    ;ELSE MOVE AWAY FROM TRACK 0
        DBF     D2,1$                   ;LOOP FOR A WHILE
2$:     BSET    #1,SAVE_D0              ;NOW MOVE TOWARD EDGE
        MOVEQ   #100,D2                 ;MAX OF 100 STEPS
        BRA.S   3$
4$:     BSR	STEP                    ;STEP THE HEADS
3$:     BTST    #4,CIAA                 ;BACK TO TRACK 0?
        DBEQ    D2,4$                   ;NO...STEP AGAIN
        ENABLE
        ZAP     D0
        MOVE.B  D0,CUR_TRACK            ;SITTING AT HOME
        MOVE.B  D0,CUR_SECTOR
        MOVE.B  D0,CUR_SIDE
        BSR	FREE_DRIVES
        RTS

DISK_CAL:
        BSR	MOTOR_ON                ;TURN MOTOR ON
	ERROR   9$                      ;DRIVE NOT READY
        BSR	HOME_DISK               ;DO THE CALIBRATE HERE
        CLR.B   CUR_TRACK               ;SHOW DISK NOW AT TRACK 0
9$:	RTS

* CLEARS TRACK SPEED VALUES FROM TPARAM_TBL.

CLR_TPARAM:
	LEA	TPARAM_TBL,A0		;ZAP THE SPEED TABLE
	ZAP	D0
	MOVEQ	#3,D1			;4 GROUPS OF TRACKS
1$:	MOVE.L	D0,12(A0)		;ZAP ACTUAL OFF_DUTY, ON_DUTY
	ADD.L	#TPAR_LEN,A0		;POINT TO NEXT ENTRY
	DBF	D1,1$
	RTS

* SELECTS DRIVE AND SIDE, AND TURNS ON MOTOR IF NECESSARY.

SELECT_DRIVE:
        BSR	BUSY_DRIVES             ;HOLD DRIVES 'TILL WE FINISH
        MOVE.B  #-1,CIAB                ;GET READY TO ISSUE DISK COMMANDS
        MOVE.B  #$7F,CIAB               ;SET MOTOR ON BIT
        MOVE.B  DRIVE_SEL,D0            ;GET DRIVE SELECT BIT CODE
	BCLR	#2,D0			;PREPARE SIDE BIT
	OR.B	NEW_SIDE,D0		;SIDE BIT (4=SIDE 0, 0=SIDE 1)
	MOVE.B  D0,CIAB                 ;SELECT DRIVE AND SIDE, MOTOR ONE
        MOVE.B  D0,SAVE_D0
        IFNZ    MOTOR_FLAG,5$           ;MOTOR ALREADY ON
        MOVE.L  #500000,D1              ;ELSE WAIT A BIT
        BSR	DELAY                   ;FOR MOTOR TO SPIN UP
5$:     RTS

* SEEKS TO PROPER TRACK, IF NECESSARY.  ASSUMES THAT DRIVE ALREADY
* SELECTED, MOTOR ON, AND INTERRUPTS OFF.

SEEK:
        ZAP     D2
        MOVE.B  NEW_TRACK,D2            ;DO WE NEED TO SEEK?
        CMP.B   CUR_TRACK,D2            ;FIGURE OUT HOW MANY STEPS ARE NEEDED
        BEQ     2$                      ;NONE NEEDED
        BHI     1$                      ;
        BSET    #1,SAVE_D0              ;MOVE TOWARD TRACK 0
        MOVE.B  CUR_TRACK,D2            ;AND CALCULATE STEPS
        SUB.B   NEW_TRACK,D2
        BRA     3$
1$:     BCLR    #1,SAVE_D0              ;MOVE TOWARD SPINDLE
        MOVE.B  NEW_TRACK,D2            ;CALC STEPS
        SUB.B   CUR_TRACK,D2
3$:     SUBQ.L  #1,D2                   ;ADJUST FOR DBF
        DISABLE
6$:     BSR	STEP                    ;DO IT
        DBF     D2,6$                   ;LOOP TILL WE GET THERE
        ENABLE
2$:     MOVE.B  NEW_TRACK,CUR_TRACK     ;NOW MAKE NEW TRACK CURRENT TRACK
        MOVE.B  NEW_SIDE,CUR_SIDE
        MOVE.B  NEW_SECTOR,CUR_SECTOR
        RTS


* STEP THE HEADS

STEP:
        MOVE.B  SAVE_D0,D0              ;SAVED DISK SELECT, SIDE, DIRECTION
        LEA     CIAB,A1
        MOVE.B  D0,D1
        BCLR    #0,D0                   ;STEP BIT
        MOVE.B  D0,(A1)
        NOP
        NOP
        MOVE.B  D1,(A1)
        MOVE.L  #$2000,D1
        BSR	DELAY                   ;WAIT
        RTS

DELAY:
        DBF     D1,DELAY
        SUB.L   #$10000,D1
        BPL.S   DELAY
        RTS

* FORMATS A SINGLE TRACK, DEFINED IN D1.  RANGE: 1 TO 70.

* WARNING - CHECK PROTECTION SOMEWHERE!

FORMAT_TRACK:
	MOVE.B	D1,DISK_MODE		;>0=WRITE
	ZAP	D0			;START WITH SECTOR 0
	BSR	SETUP_TRACK		;STORE TRACK # AND SET UP PARAMS
	MOVE.L	DUTY_PTR,A0
	IFZ	FULL_SPEED,6$
	MOVE.W	#184,D0			;NEED SOMETHING
	MOVE.W	D0,ON_DUTY
	MOVE.W	D0,OFF_DUTY
	BRA.S	1$
6$:	MOVE.W	2(A0),ON_DUTY
	MOVE.W	(A0),OFF_DUTY		;GET STORED DUTY CYCLE
	BNE.S	1$			;ALREADY ADJUSTED...DONT CHANGE
	MOVE.W	TRACK_OFF,OFF_DUTY	;GET INITIAL SETTINGS FOR THIS TRACK
	MOVE.W	TRACK_ON,ON_DUTY
1$:	BSR	SELECT_DRIVE
	BSR	SEEK			;MAKE SURE WE ARE AT THE PROPER TRACK
	BSR	ERASE_TRACK		;WRITE ENTIRE TRACK OF 55'S
	BSR	WRITE_SEC0		;WRITE HDR FOR SEC0
	BSR	BUILD_RW_HDR		;LOOK FOR THIS HEADER
	BSR	ADJUST_DUTY_CYCLE	;TRY TO ADJUST THE SPEED
	NOERROR	2$
	BSR	FREE_DRIVES
	STC
	BRA.S	9$
2$:	BSR	FREE_DRIVES		;RELEASE SO WE CAN GRAB THEM IN LOOP
3$:	BSR	BUILD_SECTOR		;BUILD SECTOR DATA AND HEADER OF NEXT
	BSR	BUILD_RW_HDR		;LOOK FOR THIS HEADER BEFORE WRITE
	MOVE.B	#5,RETRY_COUNT		;TRY 5 TIMES
4$:	BSR	SELECT_DRIVE
	BSR	DISK_WRITE		;LOCATE HEADER AND WRITE SECTOR
	NOERROR	5$			;COULDN'T FIND HDR?
	DECB	RETRY_COUNT		;TRY AGAIN?
	BNE.S	4$			;YES
	STC
	BRA.S	9$
5$:	INCB	NEW_SECTOR		;ADVANCE TO NEXT SECTOR
	MOVE.W	SEC_PER_TRACK,D0
	CMP.B	NEW_SECTOR,D0
	BNE.S	3$			;LOOP FOR ALL SECTORS
9$:	RTS

* WRITES A COMPLETE TRACK OF 55'S, ERASING WHATEVER WAS THERE BEFORE.

ERASE_TRACK:
	MOVE.L  TRACK_BUF,A2		;STORE TRACK DATA HERE
	MOVE.L	A2,A0
	MOVE.W	#FILCNT,D1		;STORE A FULL TRACK OF $55
	BSR	STORE_FILL
	MOVE.W	TRACK_SIZE,D0		;NUMBER OF WORDS TO WRITE
	BSR	DISK_GO			;DO THE WRITE
	RTS

* BUILDS A SPECIAL SECTOR 0 HEADER, AND WRITES IT TO THE TRACK.

WRITE_SEC0:
	MOVE.L	SECTOR_BUF,A2		;BUILD SPECIAL HDR HERE
	MOVEQ	#10,D1			;10 FILLS
	BSR	STORE_FILL
	BSR	STORE_SYNC		;5 SYNC BYTES
	ZAP	D0			;SECTOR 0
	BSR	BUILD_FORMAT_HDR	;BUILD ENCODED HEADER, ADD TO BUFFER
	MOVEQ	#11,D1			;FOLLOW WITH 11 FILLS
	BSR	STORE_FILL		;(TO MAKE EVEN NUMBER OF WORDS)
	MOVEQ	#18,D0			;36 BYTES
	MOVE.L	SECTOR_BUF,A0	
;	BSR	DISK_GO
;	RTS	
	
* SPECIAL FORMAT WRITE ROUTINE.  BUFFER PTR IN A0, WORD LENGTH IN D0.

DISK_GO:
	MOVE.L	A0,DSKPTR		;SET DMA ADDRESS
	MOVE.W	#$1700,ADKCON		;MAKE SURE GCR MODE
	MOVE.W	#$3002,INTREQ
	DISABLE				;GET CONTROL FOR A WHILE
	IFNZ	FULL_SPEED,2$		;DONT SLOW DOWN
	PUSH	D0			;SAVE LENGTH
	BSR	SLOW			;TRY TO SLOW THIS GUY DOWN
;	MOVE.B	#-1,CIAB
;	MOVE.B	SAVE_D0,D0
;	BSET	#7,D0
;	MOVE.B	D0,CIAB			;TURN MOTOR OFF
	POP	D0
2$:	OR.W	#$C000,D0		;MAKE IT A WRITE
	MOVE.W	D0,DSKLEN		;START THE WRITE
	MOVE.W	D0,DSKLEN
;	ENABLE
1$:	MOVE.W	INTREQR,D0		;DONE YET?
	BTST	#1,D0
	BEQ.S	1$			;LOOP TILL DONE
	MOVE.W	#$4000,DSKLEN		;STOP DMA
	MOVE.B	#$7F,CIAB
	MOVE.B	SAVE_D0,CIAB		;TURN MOTOR BACK ON
	ENABLE
	RTS

* BUILDS EMPTY SECTOR FOLLOWED BY HEADER OF FOLLOWING SECTOR

BUILD_SECTOR:
	MOVE.W	#SECLEN,WRTLEN		;WORDS IN NORMAL SECTOR
	ZAP	D3
	MOVE.B	NEW_SECTOR,D3		;WHICH IS THIS SECTOR?
	INCB	D3
	CMP.W	SEC_PER_TRACK,D3	;DONT PUT HEADER ON LAST SECTOR
	BEQ.S	1$			;OOPS...THIS IS LAST ONE
	LEA	BAMBUF+335,A2		;BAMBUF ALREADY HAS DATA
	BSR	STORE_GAP		;GAP BEFORE NEXT HEADER
	BSR	STORE_SYNC		;SYNC BYTES
	MOVE.W	D3,D0			;NUMBER OF NEXT SECTOR
	BSR	BUILD_FORMAT_HDR	;BUILD HDR FOR FOLLOWING SECTOR
	MOVEQ	#10,D1
	BSR	STORE_FILL		;FOLLOW WITH ENOUGH FILLS
	MOVE.L	A2,D0
	SUB.L	#BAMBUF,D0		;CALC LENGTH OF ALL THIS
	INCL	D0			;JUST IN CASE...
	LSR.W	#1,D0			;CONVERT TO WORDS
	MOVE.W	D0,WRTLEN		;THIS MANY WORDS
1$:	LEA	BAMBUF,A0
	MOVE.L	A0,WRTBUF		;WRITE IT OUT HERE
	RTS	

* BUILDS GCR HEADER IN NEWHDR

BUILD_RW_HDR:
	MOVE.B	NEW_SECTOR,D0		;BUILD HEADER FOR THIS SECTOR
	BSR	BUILD_HEADER
	LEA	NEWHDR,A2		;STORE ENCODED HDR HERE
	BSR	ENCODE_HEADER
	MOVE.L	#$FF000000,D1
	MOVE.L	D1,HMASK1		;ALWAYS NEED LAST 8 BITS
	AND.L	D1,NEWHDR+4
	MOVE.L	#$FFC00FFF,D1
	MOVE.L	D1,HMASK0		;PROPER MASK FOR FIRST 32 BITS
	AND.L	D1,NEWHDR
	RTS
	
* BUILDS WRITE HEADER FOR SECTOR IN D0, ENCODES AND STORES AT A2.

BUILD_FORMAT_HDR:
;	MOVE.B	D0,CUR_SECTOR
	BSR	BUILD_HEADER
	BSR	ENCODE_HEADER
	RTS

* BUILDS HEADER IN HDRBUF FOR SECTOR IN D0, AND ACTUAL TRACK.
* CALCULATES AND STORES CHECKSUM.

BUILD_HEADER:
	CLR.B	CKSUM			;CHECKSUM ACCUMULATOR
	LEA	HDRBUF,A1		;POINT TO HEADER BUFFER
	MOVE.B	#8,(A1)+		;HEADER ID
	CLR.B	(A1)+			;CHECKSUM GOES HERE
	EOR.B	D0,CKSUM
	MOVE.B	D0,(A1)+		;SECTOR
	MOVE.B	ACTUAL_TRACK,D0		;TRACK
	EOR.B	D0,CKSUM
	MOVE.B	D0,(A1)+
	MOVE.B	DISK_ID+1,D0
	EOR.B	D0,CKSUM
	MOVE.B	D0,(A1)+		;STORE ID CHAR #2
	MOVE.B	DISK_ID,D0
	EOR.B	D0,CKSUM
	MOVE.B	D0,(A1)+		;STORE ID CHAR #1
	MOVEQ	#$F,D0
	MOVE.B	D0,(A1)+		;TRAILING BYTES
	MOVE.B	D0,(A1)+
	MOVE.B	CKSUM,HDRBUF+1		;STORE CHECKSUM
	RTS

* ENCODES HEADER DATA IN HDRBUF AND STORES ENCODED DATA INTO BUFFER AT A2.

ENCODE_HEADER:
	LEA	BIN_GCR,A6
	LEA	HDRBUF,A1		;UNCODED HEADER FOUND HERE
	MOVEQ	#8,D6			;START WITH EMPTY DEST
	ZAP	D5
	MOVEQ	#7,D4			;8 BYTES IN HEADER
1$:	MOVE.B	(A1)+,D0
	BSR	PUT_BYTE		;CONVERT AND STORE BYTE
	DBF	D4,1$			;LOOP FOR ALL OF HEADER
	RTS

* ENCODES AN EMPTY BLOCK FOR USE BY FORMAT.  CALLED FROM FORMAT_DRIVE ONLY.

INIT_FORMAT:
	LEA	BLKBUF,A2		;ZAP THE BLOCK BUFFER
	MOVE.L	A2,DMAADR		;MAKE THIS THE DATA BUFFER
	MOVE.W	#256,D1
	ZAP	D0
	BSR	STORE_D0_D1_TIMES
	CLR.B	VALID_BAM		;CANT USE THIS BAM ANYMORE
	LEA	BAMBUF,A2		;STORE ENCODED SECTOR IN BAM BUFFER
	BSR.S	ENCODE_COM
	RTS

* ENCODES A BLOCK INTO GCR FORMAT, AND STORES IN TRACK_BUF.
* PTR TO BLOCK TO TRANSFER IN DMAADR.

ENCODE_SECTOR:
	MOVE.L	TRACK_BUF,A2		;RESULT TO TRACK_BUF
ENCODE_COM:
	MOVEQ	#5,D1			;5 FILL BYTES
	BSR	STORE_FILL		;FIRST SOME FILL BYTES
	BSR	STORE_SYNC		;THEN A FEW SYNC BYTES

* CONVERTS DATA AT DATA BLOCK (DMAADR) TO GCR FORMAT, AND STORES INTO
* BUFFER AT A2.  CONVERTS EXACTLY 260 BYTES (ID+256+CKSUM+2 NULLS) INTO
* 325 BYTES OF GCR-ENCODED DATA.

	CLR.B	CKSUM			;START WITH FRESH CHECKSUM
	LEA	BIN_GCR,A6		;BINARY-TO-GCR XLATE TABLE
	MOVEQ	#8,D6			;BITS PER BYTE
	ZAP	D5
	MOVEQ	#7,D0			;DATA BLOCK ID
	BSR	PUT_BYTE
	MOVE.W	#255,D4			;256 DATA BYTES...
	MOVE.L	DMAADR,A0		;POINT TO BLOCK DATA
1$:	MOVE.B	(A0)+,D0		;GET A BYTE OF DATA
	EOR.B	D0,CKSUM		;ADD TO CHECKSUM
	BSR	PUT_BYTE
	DBF	D4,1$
	MOVE.B	CKSUM,D0		;NOW STORE CHECKSUM
	BSR	PUT_BYTE
	ZAP	D0
	BSR	PUT_BYTE		;FOLLOWED BY 2 NULLS
	BSR	PUT_BYTE
	RTS

* STORES 5 SYNC BYTES INTO BUFFER

STORE_SYNC:
	MOVEQ	#5,D1
	MOVEQ	#-1,D0
	BRA.S	STORE_D0_D1_TIMES

STORE_GAP:
;	MOVE.W	GAP_COUNT,D1
	MOVEQ	#8,D1

STORE_FILL:
	MOVE.B	#$55,D0

STORE_D0_D1_TIMES:
	DECW	D1
1$:	MOVE.B  D0,(A2)+
        DBF     D1,1$
        RTS


* DECODES GCR SECTOR DATA AND MOVES TO REQUESTOR'S DATA BUFFER.
* ON EXIT, CY=1 ON DATA ERROR, CY=0 ON GOOD DATA.

DECODE_SECTOR:
	LEA	GCR_BIN,A6
	MOVE.L	TRACK_BUF,A2
	MOVE.W	#353,D2
	ZAP	D6
	ZAP	D0
	BSR	GET_NIBBLE		;GET WHAT SHOULD BE DATA MARKER
	ERROR	8$			;BAD SECTOR
	CMP.B	#7,D0			;BETTER BE 7
	BNE.S	8$			;NOT DATA MARKER...BAD FORMAT
	MOVE.L	DMAADR,A0		;COPY INTO REQUESTOR'S BUFFER
	MOVE.W  #255,D1			;256-BYTE COPY
	CLR.B	CKSUM			;ZAP THE CHECKSUM ACCUMULATOR
1$:	BSR	GET_BYT			;READ A DATA BYTE
	ERROR	8$			;BAD DATA
	MOVE.B	D0,(A0)+		;STORE IT
	EOR.B	D0,CKSUM		;BUILD RUNNING CHECKSUM
	DBF	D1,1$			;LOOP FOR WHOLE SECTOR
	BSR	GET_BYT			;GET CHECKSUM CHAR
	EOR.B	D0,CKSUM		;CHECKSUM PASS?
	BNE.S	8$			;CHECKSUM ERROR
	RTS				;YES...ALL IS WELL
8$:	STC				;ELSE SET ERROR FLAG
	RTS
	
* GETS THE NEXT DATA BYTE FROM THE DISK RAW DATA BUFFER.  RETURNS BYTE IN
* D0, WITH CY=1 ON ERROR.

GET_BYT:
	PUSH	D1-D4
	CLR.L	D0		;START EMPTY
	BSR	GET_NIBBLE	;GET FIRST NIBBLE
	ERROR	1$
	MOVE.B	D0,D4		;SAVE NIBBLE
	BSR	GET_NIBBLE	;GET OTHER NIBBLE
	ERROR	1$
	LSL.W	#4,D4
	OR.B	D4,D0
1$:	POP	D1-D4
	RTS

GET_NIBBLE:
	CLR.W	D3
	MOVEQ	#4,D1		;COUNT OF BITS TO LOAD INTO D0
1$:	TST.B	D6		;ANY BITS LEFT TO PROCESS?
	BNE.S	2$		;YES
	TST.W	D2
	BEQ.S	8$		;NOTHING LEFT IN TRACK BUFFER
	MOVE.B	(A2)+,D5	;GET NEXT RAW BYTE
	DECW	D2		;ONE LESS LEFT IN BUFFER	
	MOVEQ	#8,D6		;8 BITS TO PROCESS
2$:	ROXL.B	#1,D5		;GET NEXT DATA BIT
	ROXL.B	#1,D3		;SHIFT INTO D3
	DECB	D6		;ONE LESS BIT
	DBF	D1,1$		;LOOP FOR 5 BITS
	MOVE.B	0(A6,D3.W),D0	;TRANSLATE FROM GCR TO BINARY
	CMP.B	#-1,D0		;ILLEGAL GCR CODE?
	BEQ.S	8$		;YES...ERROR
	CLC
	RTS
8$:	STC
	RTS

* CONVERTS BYTE IN D0 TO GCR FORMAT, AND STORES THE RESULTING 10 BITS
* IN TRACK_BUFFER.  A2, D5, AND D6 ASSUMED TO HAVE BEEN PROPERLY INITED.

PUT_BYTE:
	PUSH	D1-D3
	PUSH	D0		;DO HIGH NIBBLE FIRST
	LSR.B	#4,D0		;NOW STORE LOW NIBBLE
	BSR	PUT_NIBBLE	;STORE HIGH NIBBLE
	POP	D0
	BSR	PUT_NIBBLE	;STORE LOW NIBBLE
	POP	D1-D3
	RTS

PUT_NIBBLE:
	AND.W	#$F,D0		;USE LOW NIBBLE ONLY
	MOVE.B	0(A6,D0.W),D3	;GET GCR CODE
	MOVEQ	#4,D1		;5 BITS TO STORE
1$:	ROXL.B	#1,D3		;SHIFT OUT HIGH BIT
	ROXL.B	#1,D5		;INTO BYTE ACCUMULATOR
	DECB	D6		;ONE MORE IN BYTE ACCUM
	BNE.S	2$		;MORE ROOM
	MOVE.B	D5,(A2)+	;ELSE STORE BYTE INTO TRACK BUFFER
	MOVEQ	#8,D6		;RESET BIT COUNTER
2$:	DBF	D1,1$		;LOOP FOR ALL 5 BITS IN GCR CHAR
	RTS

* REVERSES THE MOTOR-ON STATUS DURING READS

REVERSE_MOTOR:
	MOVE.B	SAVE_D0,D0
	TST.W	OFF_DUTY
	BEQ.S	2$
	EOR.B	#1,MOTOR_FLAG		;REVERSE MOTOR FLAG
	BNE.S	1$			;SHOULD BE ON NOW
	BSET	#7,D0
	BRA.S	3$
1$:	BCLR	#7,D0
3$:	MOVE.B	#-1,CIAB
	MOVE.B	D0,CIAB			;RESELECT WITH CORRECT BIT
2$:	RTS

* SETS AMIGA-DOS DISK DRIVES BUSY FOR DURATION OF DISK OPERATION.

BUSY_DRIVES:
        PUSH    A0-A1
1$:     MOVE.L  DR_BASE,A6      ;ASK AMIGA-DOS POLITELY...
        LEA     DISK_BLOCK,A1
        MOVE.L  D2D_TASK,MP_TASK
        CALL    DR_GetUnit(A6)  ;...FOR THE DISK CONTROLLER
        TST.L   D0
        BNE     2$              ;GOT THE DISK
        LEA     MSG_PORT,A0
        CALLSYS	WaitPort,SysBase ;WILL SEND REPLY TO DRU PORT
        LEA     MSG_PORT,A0
        CALLSYS	GetMsg		;UNQUEUE REPLY
        BRA.S   1$              ;NOW TRY FOR IT AGAIN
2$:     POP     A0-A1
        RTS

* FREES AMIGA-DOS DISK DRIVES AFTER DISK OPERATION.

FREE_DRIVES:
        MOVE.L  DR_BASE,A6
        CALL    DR_GiveUnit(A6) ;FREE THE DISK RESOURCE
        RTS 


DISK_INIT:
        MOVE.L  #TRKSIZ+2+SECSIZ,D0	;GRAB A BUNCH
        MOVEQ   #2,D1                   ;SPECIFY CHIP MEMORY
        CALLSYS	AllocMem,SysBase	;GRAB A HUNK OF CHIP MEMORY
        TST.L   D0                      ;GOT ANY?
        BEQ     9$                      ;NO...GIVE UP
        ADDQ.L  #2,D0                   ;ALLOW ROOM FOR ERROR
        MOVE.L  D0,TRACK_BUF            ;POINTER TO TRACK BUFFER
        ADD.L   #TRKSIZ,D0
        MOVE.L  D0,SECTOR_BUF
        LEA     DISK_NAME,A1            ;SET UP POINTER TO DISK RESOURCE
        CALLSYS	OpenResource
        MOVE.L  D0,DR_BASE              ;POINTER TO DISK RESOURCE BLOCK
        BEQ     9$                      ;DIDN'T FIND IT
        MOVE.L  #0,A1
        CALLSYS	FindTask		;GET D2D TASK POINTER
        MOVE.L  D0,D2D_TASK             ;SAVE FOR SEIZING DISK
        BEQ     9$                      ;OOPS...PROBLEM
        CLC
        RTS
9$:     STC
        RTS

DISK_EXIT:
        MOVE.L  TRACK_BUF,A1     ;BASE OF ALLOCATED MEMORY
        SUBQ.L  #2,A1
        MOVE.L  #TRKSIZ+2+SECSIZ,D0
        CALLSYS	FreeMem,SysBase	;RETURN TO POOL
        RTS

MOTOR_OFF:
        IFZ     MOTOR_FLAG,MEND  ;MOTOR OFF?
        BSR	BUSY_DRIVES      ;WAIT TILL DRIVES NOT BUSY
MOFF:   MOVE.B  #-1,CIAB         ;NOW TURN MOTOR OFF
        MOVE.B  #-1,CIAB
        MOVE.B  DRIVE_SEL,D0     ;GET DRIVE SELECT CODE
        BSET    #7,D0            ;SET MOTOR OFF BIT
        MOVE.B  D0,CIAB          ;TURN IT OFF
        CLR.B   MOTOR_FLAG       ;MOTOR OFF NOW
        BSR	FREE_DRIVES
MEND:   RTS


MOTOR_ON:
	BSR	MON		;TURN MOTOR ON, CHECK FOR DRIVE READY
	BCS.S	1$		;NOT READY
	RTS
1$:	DispErr	DRV_NR.		;DOOR NOT CLOSED, NO DISK, ETC.
	BSR	MOFF		;SHUT IT DOWN
	STC
	RTS

MON:	IFNZ    MOTOR_FLAG,9$	;MOTOR ALREADY ON?
        BSR	BUSY_DRIVES	;NO...TURN IT ON
        MOVE.B  #-1,CIAB
        MOVE.B  #-1,CIAB	;START WITH ALL RESET
        MOVE.B  #$7F,CIAB	;TURN ON MOTOR BIT
        MOVE.B  DRIVE_SEL,CIAB	;SELECT PROPER DRIVE, SIDE 0
	MOVE.L  #600000,D1	;WAIT FOR MOTOR TO COME ON
        BSR	DELAY		;WAIT FOR A WHILE
        MOVE.B  #1,MOTOR_FLAG	;MOTOR IS ON NOW
        BTST    #5,CIAA		;DRIVE READY?
        BEQ.S   8$		;YES
	LEA	DSKBYTR,A0	;DATA BYTE
	MOVE.W	#5000,D1	;TRY A FEW HUNDRED BYTES
4$:	MOVE.L	TRACK_BUF,DSKPTR ;SUPERSTITION...
	ZAP	D0
	MOVE.W	D0,DSKLEN	;TELL DRIVE TO READ
3$:	MOVE.W	(A0),D2		;TRY TO GET A DATA BYTE
	BPL.S	5$		;NO DATA
	TST.B	D2		;NULL DATA?
	BEQ.S	4$		;NULL BYTE...RESET THE COUNTER
	INCL	D0		;ELSE COUNT NON-NULL BYTE
5$:	DBF	D1,3$		;LOOP FOR A WHILE
	MOVE.W	#$4000,DSKLEN	;DISABLE DISK NOW
	CMP.W	#20,D0		;AT LEAST 20 NON-ZERO DATA BYTES IN A ROW?
	BCC.S	8$		;YES...MUST BE A DISK IN THERE
        BSR	MOFF		;TURN OFF MOTOR AND FREE DRIVES
	STC
	RTS
8$:	BSR	FREE_DRIVES	;RELEASE DISK CONTROLLER
9$:     RTS

* Binary nibble to GCR conversion table.  Shifted left by 3 for convenience.

BIN_GCR:
	DC.B	$50	;0=01010
	DC.B	$58	;1=01011
	DC.B	$90	;2=10010
	DC.B	$98	;3=10011
	DC.B	$70	;4=01110
	DC.B	$78	;5=01111
	DC.B	$B0	;6=10110
	DC.B	$B8	;7=10111
	DC.B	$48	;8=01001
	DC.B	$C8	;9=11001
	DC.B	$D0	;A=11010
	DC.B	$D8	;B=11011
	DC.B	$68	;C=01101
	DC.B	$E8	;D=11101
	DC.B	$F0	;E=11110
	DC.B	$A8	;F=10101

* GCR to binary conversion table.

GCR_BIN:
	DC.B	-1	;0
	DC.B	-1	;1
	DC.B	-1	;2
	DC.B	-1	;3
	DC.B	-1	;4
	DC.B	-1	;5
	DC.B	-1	;6
	DC.B	-1	;7
	DC.B	-1	;8
	DC.B	$8	;9
	DC.B	$0	;A
	DC.B	$1	;B
	DC.B	-1	;C
	DC.B	$C	;D
	DC.B	$4	;E
	DC.B	$5	;F
	DC.B	-1	;10
	DC.B	-1	;11
	DC.B	$2	;12
	DC.B	$3	;13
	DC.B	-1	;14
	DC.B	$F	;15
	DC.B	$6	;16
	DC.B	$7	;17
	DC.B	-1	;18
	DC.B	$9	;19
	DC.B	$A	;1A
	DC.B	$B	;1B
	DC.B	-1	;1C
	DC.B	$D	;1D
	DC.B	$E	;1E
	DC.B	-1	;1F

* TRACK PARAMETER TABLE
* IN ORDER: SECTORS/TRACK, WORDS/TRACK, GAP SIZE, TARGET SPEED COUNT,
* INITIAL OFF_DUTY, INITIAL 0N_DUTY, ACTUAL OFF_DUTY, ACTUAL ON_DUTY.

TPAR_LEN EQU	16	;LENGTH OF TABLE ENTRY

TPARAM_TBL
	DC.W	21,3948,13,3650,128,072,0,0	;TRACKS 1-17
	DC.W	19,3572,22,3400,096,104,0,0	;TRACKS 18-24
	DC.W	18,3384,17,3200,025,175,0,0	;TRACKS 25-30
	DC.W	17,3196,14,3053,000,000,0,0	;TRACKS 31-35

* DUTY CYCLE ADJUSTMENT TABLE

ADJTBL	DC.W	25,-25,50,-50,75,-75,100,-100,0

DRV_NR. 	DC.B	'C64/C128 drive not ready.',0
READ_ERR. 	DC.B	'C64/C128 drive read error.',0
PROT. 		DC.B	'C64/C128 disk write protected.',0

D2D_TASK	DC.L	0	;TASK POINTER
DR_BASE		DC.L	0	;DISK RESOURCE POINTER
DMAADR		DC.L    0	;ADDRESS WHERE SECTOR DATA GOES
SECTOR_BUF	DC.L	0	;TEMP SECTOR BUFFER
TRACK_BUF	DC.L	0	;ADDRESS OF TRACK BUFFER
WRTBUF		DC.L	0	;ADDRESS OF DMA WRITE
DUTY_PTR	DC.L	0	;POINTS TO DUTY CYCLE PARAMS IN TPARAM_TBL
HMASK0		DC.L	0	;HEADER MASK, FIRST 32 BITS
HMASK1		DC.L	0	;HEADER MASK, LAST 8 BITS
SPEED		DC.L	0	;TRACK SPEED INDICATOR
SPEED_AVG	DC.L	0	;AVERAGE TRACK SPEED
ADJPTR		DC.L	0	;POINTER TO DUTY CYCLE ADJUSTMENT TABLE
DELAY_COUNT	DC.W	0	;DUTY CYCLE LOOP COUNT
SEC_PER_TRACK	DC.W	0	;SECTORS PER TRACK
TRACK_SIZE	DC.W	0	;CURRENT TRACK SIZE IN WORDS
GAP_COUNT	DC.W	0	;COUNT OF $55s BETWEEN SECTORS
TRACK_SPEED	DC.W	0	;TARGET TRACK SPEED
TRACK_OFF	DC.W	0	;INITIAL OFF_DUTY COUNT
TRACK_ON	DC.W	0	;INITIAL ON_DUTY COUNT
OFF_DUTY	DC.W	0	;ACTUAL ON_DUTY COUNT
ON_DUTY		DC.W	0	;ACTUAL OFF_DUTY COUNT
WRTLEN		DC.W	0	;NUMBER OF WORDS FOR DMA WRITE
CKSUM		DC.B	0	;CHECKSUM ACCUMULATOR
DISK_NAME	DC.B	'disk.resource',0
DISK_MODE	DC.B	0       ;1=WRITE, 0=READ
SAVE_D0		DC.B	0       ;SAVED DRIVE SELECT, SIDE, DIRECTION
MOTOR_FLAG	DC.B	0       ;1=MOTOR ON, 0=OFF
DRIVE_SEL	DC.B	0       ;C/C DRIVE, CODED FOR AMIGA
CUR_TRACK	DC.B	0       ;CURRENT TRACK
CUR_SIDE	DC.B	0       ;CURRENT SIDE (4=SIDE 0, 0=SIDE 1)
CUR_SECTOR	DC.B	0       ;CURRENT SECTOR
NEW_TRACK	DC.B	0       ;REQUESTED TRACK
NEW_SIDE	DC.B	0       ;REQUESTED SIDE
NEW_SECTOR	DC.B	0       ;REQUESTED SECTOR
ACTUAL_TRACK	DC.B	0	;ACTUAL TRACK REQUESTED
RETRY_COUNT	DC.B	0       ;READ RETRY COUNTER
FULL_SPEED	DC.B	0	;0=SET SPEED, >0=FULL SPEED

DUTY_TBL

* DISK RESOURCE UNIT STRUCTURE

INT_STRUCT MACRO
        NODE 2,0,0
        DC.L    0
        DC.L    0
        ENDM

        CNOP  0,4

DISK_BLOCK
        NODE    5,0,0
DB_PORT DC.L    MSG_PORT
        DC.W    DB_MLEN
        INT_STRUCT
        INT_STRUCT
        INT_STRUCT
DB_MLEN EQU *-DISK_BLOCK

MSG_PORT:
        NODE    4,0,0
        DC.B    0       ;FLAGS=SIGNAL
        DC.B    8       ;SIGNAL 8
MP_TASK DC.L    0       ;MY TASK
MP_HEAD DC.L    MP_TAIL ;HEAD
MP_TAIL DC.L    0       ;TAIL
        DC.L    MP_HEAD ;TAIL PRED
        DC.B    5       ;TYPE
        DC.B    0       ;PAD
HDRBUF	DCB.B	16,0	;SECTOR HEADER
NEWHDR	DCB.B	16,0	;ENCODED DESIRED SECTOR HEADER


        END