PERFORCE change 94896 for review

Warner Losh imp at FreeBSD.org
Mon Apr 10 04:17:27 UTC 2006


http://perforce.freebsd.org/chv.cgi?CH=94896

Change 94896 by imp at imp_hammer on 2006/04/10 04:16:57

	You don't need a complicated state machine for xmodem, at least
	the limited subset we're doing.  Optimize things a bit.  This
	gets us another 50 bytes: 1164 bytes.  1024 in striking distance, but
	those last 140 bytes are going to be very hard.  Good think we have
	about 11k to run in :-).

Affected files ...

.. //depot/projects/arm/src/sys/boot/arm/kb920x/boot0/xmodem.c#2 edit

Differences ...

==== //depot/projects/arm/src/sys/boot/arm/kb920x/boot0/xmodem.c#2 (text+ko) ====

@@ -43,11 +43,6 @@
 	SEND_NAK
 };
 
-static char	packetNumber;
-
-#define TransitionState(x, y) (x = y)
-
-
 /*
  * .KB_C_FN_DEFINITION_START
  * int GetRecord(char , char *)
@@ -55,25 +50,27 @@
  * returns non-zero on success.
  * .KB_C_FN_DEFINITION_END
  */
-static int GetRecord(char blocknum, char *dest) {
+static int
+GetRecord(char blocknum, char *dest)
+{
 	int		size;
 	char		nextChar;
 	unsigned	chk, j;
 
 	chk = 0;
 
-	if (!WaitForChar(&nextChar, 1)) {
-		return (0);
-	}
-
-	if ((char)nextChar != (char)~blocknum) {
-		return (0);
-	}
-
+	if (!WaitForChar(&nextChar, 1))
+		goto err;
+	if (nextChar != blocknum)
+		goto err;
+	if (!WaitForChar(&nextChar, 1))
+		goto err;
+	if ((char)nextChar != (char)~blocknum)
+		goto err;
+	
 	for (size = 0; size < PACKET_SIZE; ++size) {
-		if (!WaitForChar(&nextChar, 1)) {
-			return (0);
-		}
+		if (!WaitForChar(&nextChar, 1))
+			goto err;
 		chk = chk ^(int)nextChar << 8;
 		for (j = 0; j < 8; ++j) {
 			if (chk & 0x8000)
@@ -86,17 +83,17 @@
 
 	chk &= 0xFFFF;
 
-	if ((!WaitForChar(&nextChar, 1)) || (nextChar != ((chk >> 8) & 0xFF))) {
-		return (0);
-	}
-
-	if ((!WaitForChar(&nextChar, 1)) || (nextChar != (chk & 0xFF))) {
-		return (0);
-	}
-
+	if ((!WaitForChar(&nextChar, 1)) || (nextChar != ((chk >> 8) & 0xFF)))
+		goto err;
+	if ((!WaitForChar(&nextChar, 1)) || (nextChar != (chk & 0xFF)))
+		goto err;
 	DebugPutc(ACK);
 
 	return (1);
+err:;
+	DebugPutc(NAK);
+	// We should allow for resend, but we don't.
+	return (0);
 }
 
 
@@ -111,60 +108,31 @@
  * -1 on error.
  * .KB_C_FN_DEFINITION_END
  */
-int xmodem_rx(char *dest) {
+int
+xmodem_rx(char *dest)
+{
+	int		starting;
+	char		packetNumber, nextChar, *startAddress = dest;
 
-	int		state;
-	char		nextChar, *startAddress = dest;
-
 	packetNumber = 1;
-	state = WAITING_START;
+	starting = 1;
 
 	while (1) {
-
-		if (state == WAITING_START) {
+		if (starting)
 			DebugPutc('C');
-			if (WaitForChar(&nextChar, 1)) {
-				if (nextChar == SOH) {
-					TransitionState(state, RX_PACKET);
-				}
-			}
+		if (!WaitForChar(&nextChar, 1) || 
+		    (nextChar != SOH && nextChar != EOT))
+			continue;
+		if (nextChar == EOT) {
+			DebugPutc(ACK);
+			return (dest - startAddress);
 		}
-
-		if (state == WAIT_SOH) {
-			if (!WaitForChar(&nextChar, 1)) {
-				return (-1);
-			}
-
-			if (nextChar == SOH) {
-				TransitionState(state, RX_PACKET);
-			}
-
-			if (nextChar == EOT) {
-				// TransitionState(state, RX_EOT);
-				DebugPutc(ACK);
-				return (dest - startAddress);
-			}
-		}
-
-		if (state == RX_PACKET) {
-			if (!WaitForChar(&nextChar, 1)) {
-				return (-1);
-			}
-
-			if (nextChar != packetNumber) {
-				// TransitionState(state, SEND_NAK);
-				DebugPutc(NAK);
-				return (-1);
-			}
-
-			if (GetRecord(packetNumber, dest)) {
-				dest += PACKET_SIZE;
-				++packetNumber;
-				TransitionState(state, WAIT_SOH);
-			} else {
-				return (-1);
-			}
-		}
+		starting = 0;
+		// Xmodem packets: SOH PKT# ~PKT# 128-bytes CRC16
+		if (!GetRecord(packetNumber, dest))
+			return (-1);
+		dest += PACKET_SIZE;
+		packetNumber++;
 	}
 
 	// the loop above should return in all cases


More information about the p4-projects mailing list