\documentclass[10pt]{beamer}

\usetheme[progressbar=frametitle]{metropolis}
\usepackage{appendixnumberbeamer}

\usepackage{booktabs}
\usepackage[scale=2]{ccicons}

\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}

\usepackage{xspace}
\usepackage{minted}
%\usepackage{tabularx}
\usepackage{bytefield}
\newcommand{\themename}{\textbf{\textsc{metropolis}}\xspace}
\setminted[]{autogobble, breaklines, breakafter=-/,fontsize=\footnotesize}
\title{C - Bit Fields, xv6 Setup, gdb} 
\subtitle{CS238P: Principles of operating systems - Fall'18}
% \date{\today}
\date{October 26, 2018}
\author{Aftab Hussain \\ (Adapted from Vikram Narayanan's CS143A Fall '17 slides)}
\institute{University of California, Irvine}
% \titlegraphic{\hfill\includegraphics[height=1.5cm]{logo.pdf}}

\begin{document}

\maketitle



% \fi

% String Copy Function
% //An assignment expression has the value of the left operand after the assignment.
% http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
% https://stackoverflow.com/questions/810129/how-does-whiles-t-copy-a-string
% http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf (page 101, Section 6.5.16)


% /* strcpy: copy t to s; pointer version 2 */
% void strcpy(char *s, char *t)
% {
%   while (*s++ = *t++);
% }



% \begin{frame}{Dynamic registration}
%   \small
%   \begin{itemize}
%   \item<1-> Declare a struct to hold function pointers~\footnote{sheet 40 xv6-rev9.pdf}
%   \begin{minted}[fontsize=\footnotesize]{c}
%   #define NDEV  10
%   #define CONSOLE 1
%   struct devsw {
%     int (*read)(struct inode*, char*, int);
%     int (*write)(struct inode*, char*, int);
%   };
%   struct devsw devsw[NDEV]; /* global data structure */
%   \end{minted}
%   \item<2-> Register function pointer~\footnote{sheet 82 xv6-rev9.pdf}
%   \begin{minted}{c}
%   int consolewrite(struct inode *ip, char *buf, int n);
%   int consoleread(struct inode *ip, char *dst, int n);
%   devsw[CONSOLE].write = consolewrite;
%   devsw[CONSOLE].read = consoleread;
%   \end{minted}
%   \end{itemize}
%   \end{frame}


\begin{frame}[standout]
  Bits \\
  \end{frame}

  \begin{frame}[standout]
    A data structure to hold bits
    \end{frame}

\begin{frame}[fragile=singleslide]{Bit fields\footnote{sheet 09 xv6-rev9.pdf}}
\begin{minted}{c}
// Gate descriptors for interrupts and traps
struct gatedesc {
  uint off_15_0 : 16; // low 16 bits of offset in segment
  uint cs : 16; // code segment selector
  uint args : 5; // # args, 0 for interrupt/trap gates
  uint rsv1 : 3; // reserved(should be zero I guess)
  uint type : 4; // type(STS_{TG,IG32,TG32})
  uint s : 1; // must be 0 (system)
  uint dpl : 2; // descriptor(meaning new) privilege level
  uint p : 1; // Present
  uint off_31_16 : 16; // high bits of offset in segment
};

struct gatedesc d;
d.s = 0; d.args = 0;

\end{minted}

\end{frame}

\begin{frame}[fragile=singleslide]{Bit fields}
  
  ``In C and C++, native implementation-defined bit fields can be created using unsigned int, signed int, or (in C99:) \_Bool."\footnote{https://en.wikipedia.org/wiki/Bit\_field}

    
  \end{frame}

\begin{frame}[fragile]{Access low-level data}
\begin{bytefield}[endianness=little,bitwidth=1em]{32}
\bitheader[lsb=32]{32,36,37,39,40,43,44,45,46,47,48,63} \\
\bitbox{5}{args} & \bitbox{3}{rsvd} & \bitbox{4}{type} & \bitbox{1}{\textcolor{red}{s}} & \bitbox{2}{dpl} & \bitbox{1}{p} & \bitbox{16}{offset high} \\ [3ex]
\bitheader{0,15,16,31} \\
\bitbox{16}{offset} & \bitbox{16}{segment} \\
\end{bytefield}
\begin{itemize}
\item<1-> Set bit 44 (s) to 1, without changing anything else.
\begin{minted}{c}
/* on a 64-bit data type */
data = data | (1 << 44); //the same as under
data |= (1 << 44); 
\end{minted}
\item<2-> Clear a bit (s) - And ($\&$) and Not ($\sim$) 
\begin{minted}{c}
/* on a 64-bit data type */
data = data & ~(1 << 44);
data &= ~(1 << 44);
\end{minted}
\end{itemize}
\end{frame}






% \begin{frame}[fragile]{Pointers \& buffer management}
% \begin{itemize}
% \item<1-> Access raw memory
% \begin{minted}{c}
% #define KERNBASE 0x80000000
% #define P2V(a) (((void *) (a)) + KERNBASE)
% uchar *code;
% code = P2V(0x7000);
% \end{minted}
% \item<2-> kalloc, memset, kfree
% \begin{minted}{c}
% mem = kalloc(); /* allocate a page */
% memset(mem, 0, PGSIZE); /* memset */
% kfree(mem); /* free it when done */
% \end{minted}
% \item<3-> memcpy, memmove
% \begin{minted}{c}
% /* move start to code */
% memmove(code, _binary_entryother_start,
%       (uint)_binary_entryother_size);
% \end{minted}
% \end{itemize}
% \end{frame}


%\setbeamercolor{palette primary}%{fg=black, bg=yellow}
\begin{frame}[standout]
  Moving on to xv6 setup and gdb demo.
\end{frame}

\end{document}
