For this assignment, you will implement a collection of C functions to support a classic data encoding scheme.

Similar documents
gcc o driver std=c99 -Wall driver.c bigmesa.c

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

Creating a String Data Type in C

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

Pointer Accesses to Memory and Bitwise Manipulation

Pointer Casts and Data Accesses

struct _Rational { int64_t Top; // numerator int64_t Bottom; // denominator }; typedef struct _Rational Rational;

A Capacity: 10 Usage: 4 Data:

Pointer Accesses to Memory and Bitwise Manipulation

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

gcc o driver std=c99 -Wall driver.c everynth.c

Both parts center on the concept of a "mesa", and make use of the following data type:

The assignment requires solving a matrix access problem using only pointers to access the array elements, and introduces the use of struct data types.

Pointer Accesses to Memory and Bitwise Manipulation

You will provide an implementation for a test driver and for a C function that satisfies the conditions stated in the header comment:

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

Simple C Dynamic Data Structure

Each line will contain a string ("even" or "odd"), followed by one or more spaces, followed by a nonnegative integer.

CS 1044 Program 6 Summer I dimension ??????

Given that much information about two such rectangles, it is possible to determine whether they intersect.

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

PR quadtree. public class prquadtree< T extends Compare2D<? super T> > {

A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of two opposite corners:

Decision Logic: if, if else, switch, Boolean conditions and variables

Error Detection. Hamming Codes 1

The Program Specification:

For storage efficiency, longitude and latitude values are often represented in DMS format. For McBryde Hall:

CS 1044 Program 2 Spring 2002

CS 142 Style Guide Grading and Details

CS 2505 Computer Organization I

Do not start the test until instructed to do so!

Error Detection And Correction

CS 2604 Minor Project 1 DRAFT Fall 2000

Error-Correcting Codes

Type Checking and Type Equality

Chapter 10 Error Detection and Correction 10.1

Overview. Exercise 0: Implementing a Client. Setup and Preparation

Overview. Exercise 0: Implementing a Client. Setup and Preparation

CS 2604 Minor Project 1 Summer 2000

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Fundamental Concepts: array of structures, string objects, searching and sorting. Static Inventory Maintenance Program

My malloc: mylloc and mhysa. Johan Montelius HT2016

CS 385 Operating Systems Fall 2011 Homework Assignment 5 Process Synchronization and Communications

Lecture Notes on Memory Layout

CS 610: Intermediate Programming: C/C++ Making Programs General An Introduction to Linked Lists

QUIZ. What are 3 differences between C and C++ const variables?

EL6483: Brief Overview of C Programming Language

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

Hamming Codes. s 0 s 1 s 2 Error bit No error has occurred c c d3 [E1] c0. Topics in Computer Mathematics

CS 1044 Project 2 Spring 2003

Errors. Chapter Extension of System Model

Accessing Data in Memory

CS 1044 Project 1 Fall 2011

Lab 4 Prelab: MIPS Function Calls

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-32 assembly code is produced:

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Goals of this Lecture

Instantiation of Template class

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

CMSC 2833 Lecture 18. Parity Add a bit to make the number of ones (1s) transmitted odd.

Divisibility Rules and Their Explanations

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced:

CS 3114 Data Structures and Algorithms DRAFT Minor Project 3: PR Quadtree

CS 2505 Computer Organization I

Bits, Words, and Integers

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so!

CS 2604 Minor Project 3 Movie Recommender System Fall Braveheart Braveheart. The Patriot

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Do not start the test until instructed to do so!

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

Submit your answers to these questions to the Curator under Quizzes as HW08 by the posted due date and time. No late submissions will be accepted.

FAULT TOLERANT SYSTEMS

int32_t Buffer[BUFFSZ] = {-1, -1, -1, 1, -1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, -1, -1, -1, -1, -1}; int32_t* A = &Buffer[5];

CS 361 Computer Systems Fall 2017 Homework Assignment 1 Linking - From Source Code to Executable Binary

CS 2704 Project 1 Spring 2001

CS2304 Spring 2014 Project 3

Memory and Pointers written by Cathy Saxton

Motivation was to facilitate development of systems software, especially OS development.

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CIT 590 Homework 5 HTML Resumes

Lecture Notes on Generic Data Structures

Physics 234: Lab 7 Tuesday, March 2, 2010 / Thursday, March 4, 2010

Deep C (and C++) by Olve Maudal

Module 10: Imperative Programming, Modularization, and The Future

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

CS355 Hw 4. Interface. Due by the end of day Tuesday, March 20.

18-642: Code Style for Compilers

CS 2604 Minor Project 3 DRAFT Summer 2000

Final Examination --- SAMPLE SOLUTIONS May

Data Management, Spring 2015: Project #1

Exercise Session 2 Systems Programming and Computer Architecture

Section Notes - Week 1 (9/17)

Do not start the test until instructed to do so!

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

(Refer Slide Time: 1:40)

Cosc 242 Assignment. Due: 4pm Friday September 15 th 2017

Transcription:

C Programming SEC-DED Data Encoding For this assignment, you will implement a collection of C functions to support a classic data encoding scheme. Data transmission and data storage both raise the risk that one or more bits will be corrupted (received or stored incorrectly). Various schemes have been devised for detecting, and even correcting, such errors. It should be noted that any such scheme can be overcome if enough bits are corrupted. Richard Hamming devised an ingenious scheme that provides simultaneously for the correction of corruption involving a single bit (SEC), and detection, but not correction, of corruption involving two bits (DED). The system will, however, fail if more than two bits are corrupted. Given a sequence of data bits, we will compute a corresponding set of parity-check bits (or simply parity bits). The general idea is that the parity bits are intermingled with the data bits as follows: Parity bits are stored at index 0, and at indices that are powers of 2 (1, 2, 4, etc.). Data bits are stored (in their original order) at the indices not occupied by parity bits. Each parity bit indicates whether a particular set of data bits contains an even or an odd number of 1's; a parity bit of 0 indicates an even number of 1's and a parity bit of 1 indicates an odd number of 1's (in the corresponding set of data bits). The sets for the parity bits are defined as follows (think of the bit positions being represented in binary): The "master" parity bit at index 0 corresponds to all the other bits, including parity bits. The parity bit at index 1 corresponds to the bits whose positions have their 1's bit set. The parity bit at index 2 corresponds to the bits whose positions have their 2's bit set.... The parity bit at index 2 k corresponds to the bits whose positions have their 2 k 's bit set. Put differently: The parity bit at index 1 corresponds to bits at the positions: 3 5 7 9 11 13 15 17 19 21... The parity bit at index 2 corresponds to bits at the positions: 3 6 7 10 11 14 15 18 19 22... The parity bit at index 4 corresponds to bits at the positions: 5 6 7 12 13 14 15 20 21 22... Again, consider the bit positions represented in binary, and you will find a simple way to use bitwise operations to determine if a data bit at a given position corresponds to a parity bit at a given position. As far as error detection goes, the ingenuity of Hamming's scheme lies in several facts (we ignore the "master" parity bit, and assume no more than one bit is corrupted): Each data bit affects at least two parity bits, and no parity bit affects another parity bit. Therefore, if a single bit is corrupted, we can tell whether it is a parity bit or a data bit. If one parity bit disagrees with its data bits, and all the other parity bits agree with their data bits, then the error lies in that single parity bit. If two or more parity bits disagree with their data bits (and no more than one bit is in error), there will be a single data bit that affects all of the disagreeable parity bits, and that must be the erroneous bit. Even better, if we know which parity bits are disagreeable, the sum of their indices equals the index of the erroneous data bit. We will call the sum of the disagreeable parity bits the syndrome. This is a distance-3 coding scheme; in other words, if we start with a valid set of data and parity bits (i.e., one in which all the bits are correct), at least three bits must flip in order to yield a different, valid set of data and parity bits. However, the facts above are not enough to provide a satisfactory system; it supports detection of single-bit errors (and even double-bit errors), but it cannot distinguish a single-bit error from a double-bit error. Version 3.00 This is a purely individual assignment! 1

Therefore, it leaves us with two alternatives: use the scheme to detect errors (in up to two bits) but not to attempt any corrections, or assume that no more than a single bit will ever be wrong, and use the scheme to correct single-bit errors. The first alternative seems weak, the second is dangerous. In other words, this scheme gives us DED, but not reliable SEC. The next step is to add the aforementioned master parity bit, which measures the parity of all the other bits. This is sometimes referred to as an extended Hamming code. The additional parity bit provides a surprising benefit: we can now distinguish between single-bit and double-bit errors. The underlying theory is beyond this specification, but the following facts hold: Possible Scenarios Errors Overall Parity Syndrome Conclusion 0 even 0 no error 1 odd 0 not 0 master parity bit is erroneous syndrome indicates erroneous bit 2 even not 0 double-bit error (not correctable) Note: do not confuse "overall parity" with "master parity bit". The overall parity is the parity of the entire sequence of bits, including all the data bits and all of the parity bits. For the following exercise, we will assume that bit corruption is sufficiently unlikely that we may disregard the possibility of more than two bits being corrupted. In principle, Hamming's scheme can be used to encode an arbitrarily long sequence of data bits. Pragmatically, we need to fit things into an integer number of bytes. For this assignment, you will implement the necessary functions for encoding a sequence of 64 data bits, which will require 8 parity bits, for a total of 72 bits. This is called a Hamming (72, 64) code; the convention is that the first value is the total length of the encoded data and the second is the number of data bits. Since this does not fit exactly into any of the standard C types, we will employ the following struct type: #define NBYTES 9 number of bytes (data + parity) struct _Hamming72_64 { uint8_t bits[nbytes]; 72-bit data/parity chunk }; typedef struct _Hamming72_64 Hamming72_64; Conceptually, the array is viewed as a monolothic chunk of bits. Suppose we have 8 bytes of data, which look like this when printed: byte: 0 1 2 3 4 5 6 7 data: 62 4E F9 FE 7A CC E5 7B The bits of the Hamming encoding are stored in the array in ascending order: Bit positions: Lo Hi 00000000 00111111 11112222 22222233 33333333 44444444 44555555 55556666 66666677 01234567 89012345 67890123 45678901 23456789 01234567 89012345 67890123 45678901 -------------------------------------------------------------------------------- Data: 01000110 01110010 10011111 01111111 01011110 00110011 10100111 11011110 Encoded: 00001100 00110011 11001010 01111101 11111110 10111100 01100111 01001111 11011110 Parity: ^^^ ^ ^ ^ ^ ^ If the bit-ordering is confusing, remember that numeric values print in big-endian order; so, the first byte of data prints as "62", while the low nybble is 0010 and the high nybble is 0110. The display of the bits, on the other hand, is purely littleendian (at the bit level within a byte), so the bits seem to be backwards, but the order shown is actually the natural ordering that would occur in little-endian hardware. Version 3.00 This is a purely individual assignment! 2

You might wonder why we wrap the array in a struct variable with no other members. The advantages are that we now have a specific data type for representing Hamming encodings, and the convenience that we can assign struct variables and use them as return values (neither of which is possible with a naked array in C). The first function we need is one that can take a sequence of data bits, compute the corresponding parity bits, and assemble a proper object to represent the encoded data: /** Create the Hamming encoding for a sequence of 64 data bits. * * Pre: *ph is a Hamming72_64 object * *pbits is a block of 64 bits * Post: ph->bits has been initialized to store the given data bits and * corresponding parity bits, as described in the specification * of the Hamming (72, 64) scheme. */ void set_hamming72_64(hamming72_64* const ph, const uint8_t* const pbits); We will test this function by passing it pointers to various Hamming72_64 objects and then examining the contents of ph->bits (both the data and parity bits). This function should be relatively easy for you to test, because it's easy to vary the data bits that correspond to each parity bit, and make sure that the parity bits vary correctly. That said, it does help if you can view the bits in a human-friendly manner. The second function we need is one that can take a Hamming72_64 object, determine if any errors can be detected, whether any such errors can be corrected, and correct them, if possible. We will also need a way for the function to report its findings in detail to the caller; so we will employ the following enum and struct types: enum _Error {MASTERPARITY, SINGLEPARITY, SINGLEDATA, MULTIBIT, NOERROR}; typedef enum _Error Error; struct _Report_Hamming72_64 { Error status; uint8_t syndrome; }; typedef struct _Report_Hamming72_64 Report_Hamming72_64; The analysis and correction function will have the following specification: /** Checks the validity of the given Hamming encoding, and corrects errors * when possible. * * Pre: *ph is initialized * Post: If correctable errors are found, they are corrected in ph->bits. * Returns: A Report_Hamming72_64 object holding the computed syndrome * and flag indicating what type of error, if any, was found. */ Report_Hamming72_64 correct_hamming72_64(hamming72_64* const ph); A C header file containing these declarations is supplied on the course website; do not modify the contents of that file for any reason, since your submission will be compiled using the posted version of the header file. You should consider making your solution modular by writing helper functions; my solution uses three for computing/checking parity bits, in addition to the print function mentioned below. Version 3.00 This is a purely individual assignment! 3

Testing A tar file is posted on the course website with the following contents: driver.c Hamming72_64.c Hamming72_64.h HammingGrader.h HammingGrader.o calls test functions from HammingGrader.o shell file for implementing Hamming72_64 type public declarations for Hamming72_64 type public declarations of test functions; pay attention to this one! implementations of test functions You should edit the file Hamming72_64.c and complete the two specified functions there (as well as any static helper functions or include directives you want to add to that file). You may edit the file driver.c if you want more control over the testing, but be sure to test your final solution with the original version of driver.c since that is how we will test your solution. You must not change either of the posted header files (Hamming72_64.h and HammingGrader.h). You can compile the code using the command gcc o driver std=c99 Wall ggdb3 driver.c Hamming72_64.c HammingGrader.o and execute it using the command driver [-rand] The switch rand is optional (but must be used the first time you run the code). Using the switch causes the driver to randomize the test data; omitting it causes the test driver to reuse the previous test data. The driver writes an output file, HammingLog.txt, which contains results and score information. Some sample files will be posted showing the correct parity bits for selected data bit sets. You may use those samples as the basis for your analysis; however, they are not guaranteed to be comprehensive. You will find that implementation and testing are both much easier if you write some specialized helper functions. My implementation of the Hamming72_64 type contains 8 static functions, including: static uint8_t getbit(const uint8_t* const pb, uint8_t pos); static void flipbit(uint8_t* const pb, uint8_t pos); static bool ispowerof2(uint8_t N); static void printbits(file* fp, const Hamming72_64* const ph); There are no suitable functions in the C math library, and you should not call any from your solution. Be aware that we will not compile your solution with the lm switch. Suggested resources You should consider the following notes from the CS 2505 course website (courses.cs.vt.edu/~cs2505/summer2016/): C Bitwise Operations Intro to Pointers C Pointer Finale C struct Types http:courses.cs.vt.edu/cs2505/summer2015/notes/ T13_CBitwiseOperators.pdf http:courses.cs.vt.edu/cs2505/summer2015/notes/t14_intropointers.pdf http:courses.cs.vt.edu/cs2505/summer2015/notes/t17_cpointerfinale.pdf http:courses.cs.vt.edu/cs2505/summer2015/notes/t24_cstructtypes.pdf Some of the other notes on the basics of C and separate compilation may also be useful. Version 3.00 This is a purely individual assignment! 4

What to Submit You will submit a single C implementation file, Hamming72_64.c, containing your implementations of the two specified functions, and any static helper functions your solution needs, and nothing else. Note that any changes you might have made to the posted header file, Hamming72_64.h, will be lost since we will use the original version in our testing. Your submission will be compiled with the testing code using the gcc switches: Test your solution thoroughly before submitting it. gcc std=c99 ggdb3 Wall... The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at: Pledge: http:www.cs.vt.edu/curator/ Each of your program submissions must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement in the submitted file: On my honor: - I have not discussed the C language code in my program with anyone other than my instructor or the teaching assistants assigned to this course. - I have not used C language code obtained from another student, or any other unauthorized source, either modified or unmodified. - If any C language code or documentation used in my program was obtained from another source, such as a text book or course notes, that has been clearly noted with a proper citation in the comments of my program. <Student Name> Change Log Version Posted Pg Change 3.00 Nov 17 Base document. Version 3.00 This is a purely individual assignment! 5